Trabajo Final Seguridad Vial

Alumna: Mora Assereto Farroni

Objetivo: El objetivo del trabajo es encontrar características urbanas para determinar la vulnearbilidad de los usuarios viales. De este modo se busca evaluar qué políticas públicas son más eficacespara alcanzar nivel de fatalidad y accidentes de tránsito violentos cero

Entendimiento de datos

El modelo se basa en una serie de variables Variables independientes son:

  • DenPob: densidad poblacional (población/km2)

  • ArCiclista: relación entre el área total para ciclistas y el área total para conducir.

  • ArBajaVel: relación entre el área total limitada para baja velocidad y el área total para conducir.

  • PMPeatones: participación modal para caminar(%).

  • PMCiclistas: participación modal para cyclistas (%).

  • PMTPublico: participación modal para transporte público (%).

  • PMVMotor: participación modal para vehículos con motor (%)

  • Temp: temperatura media anual (C).

  • Precipitacion: precipitación media anual (mm).

  • PBI: producto bruto interno medio per cápita (EUR).

Por otra parte, como variables de respuesta tenemos el índice MDS que provocan choques del vehículo A al B (A → B), lo notamos AB.

  • PeatAuto: Peaton → Auto o Taxi.

  • CicAuto: Ciclista → Auto o Taxi.

  • V2RMSM :Vehículo con dos ruedas a motor → si mismos.

  • V2RMAuto Vehículo con dos ruedas a motor → Auto o Taxi.

  • AutoSM Auto o Taxi → si mismos.

  • AutoAuto Auto o Taxi → Auto o Taxi.

Librerias

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(GGally)
## Loading required package: ggplot2
## Registered S3 method overwritten by 'GGally':
##   method from   
##   +.gg   ggplot2
library(ggplot2)
library(tidyverse)
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.1 ──
## ✔ tibble  3.1.7     ✔ purrr   0.3.4
## ✔ tidyr   1.2.0     ✔ stringr 1.4.0
## ✔ readr   2.1.2     ✔ forcats 0.5.1
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
library(hrbrthemes)
## NOTE: Either Arial Narrow or Roboto Condensed fonts are required to use these themes.
##       Please use hrbrthemes::import_roboto_condensed() to install Roboto Condensed and
##       if Arial Narrow is not on your system, please see https://bit.ly/arialnarrow
library(ggpubr)
library(cowplot)
## 
## Attaching package: 'cowplot'
## The following object is masked from 'package:ggpubr':
## 
##     get_legend
library(caTools)
library(corrplot)
## corrplot 0.92 loaded
library(Hmisc)
## Loading required package: lattice
## Loading required package: survival
## Loading required package: Formula
## 
## Attaching package: 'Hmisc'
## The following objects are masked from 'package:dplyr':
## 
##     src, summarize
## The following objects are masked from 'package:base':
## 
##     format.pval, units
library(multcomp)
## Loading required package: mvtnorm
## Loading required package: TH.data
## Loading required package: MASS
## 
## Attaching package: 'MASS'
## The following object is masked from 'package:dplyr':
## 
##     select
## 
## Attaching package: 'TH.data'
## The following object is masked from 'package:MASS':
## 
##     geyser
library(readxl)
library(gmodels)
library(ggthemes)
## 
## Attaching package: 'ggthemes'
## The following object is masked from 'package:cowplot':
## 
##     theme_map
library(devtools)
## Loading required package: usethis
library(car)
## Loading required package: carData
## 
## Attaching package: 'car'
## The following object is masked from 'package:purrr':
## 
##     some
## The following object is masked from 'package:dplyr':
## 
##     recode
library(leaflet)
library(psych)
## 
## Attaching package: 'psych'
## The following object is masked from 'package:car':
## 
##     logit
## The following object is masked from 'package:Hmisc':
## 
##     describe
## The following objects are masked from 'package:ggplot2':
## 
##     %+%, alpha
library(skimr)
library(DataExplorer)
library(scales)
## 
## Attaching package: 'scales'
## The following objects are masked from 'package:psych':
## 
##     alpha, rescale
## The following object is masked from 'package:purrr':
## 
##     discard
## The following object is masked from 'package:readr':
## 
##     col_factor
library(corrr)
## 
## Attaching package: 'corrr'
## The following object is masked from 'package:skimr':
## 
##     focus
library(glmnet)
## Loading required package: Matrix
## 
## Attaching package: 'Matrix'
## The following objects are masked from 'package:tidyr':
## 
##     expand, pack, unpack
## Loaded glmnet 4.1-4
library(pls)
## 
## Attaching package: 'pls'
## The following object is masked from 'package:corrplot':
## 
##     corrplot
## The following object is masked from 'package:stats':
## 
##     loadings
library(readxl)

Recoleción de datos

segvial <- read_excel("SegVial.xlsx")
segvial
## # A tibble: 24 × 20
##    `Codigo Pais` Pais   Ciudad  Poblacion DenPob ArCiclista ArBajaVel PMPeatones
##    <chr>         <chr>  <chr>       <dbl>  <dbl> <chr>      <chr>     <chr>     
##  1 ES            Spain  Barcel…   1620343  16014 0.15       0.775     32.0      
##  2 ES            Spain  Madrid    3223334   5332 0.067      0.065     34.0      
##  3 FR            France Bordea…    254436   5104 0.106      0.239     21.0      
##  4 FR            France Lille      232787   6687 0.098      0.634     32.0      
##  5 FR            France Lyon       516092  10758 0.109      0.284     34.0      
##  6 FR            France Marsei…    863310   3564 0.044      0.118     34.0      
##  7 FR            France Montpe…    285121   4997 0.167      0.125     26.0      
##  8 FR            France Nantes     309346   4704 0.327      0.354     27.0      
##  9 FR            France Nice       340017   4586 0.056      0.102     40.0      
## 10 FR            France Paris     2187526  20772 0.186      0.491     47.0      
## # … with 14 more rows, and 12 more variables: PMCiclistas <chr>,
## #   PMTPublico <chr>, PMVMotor <chr>, PeatAuto <dbl>, CicAuto <dbl>,
## #   V2RMSM <dbl>, V2RMAuto <dbl>, AutoSM <dbl>, AutoAuto <dbl>,
## #   Precipitacion <chr>, Temp <chr>, PBI <dbl>
attach(segvial)

Explorar los datos

glimpse(segvial)
## Rows: 24
## Columns: 20
## $ `Codigo Pais` <chr> "ES", "ES", "FR", "FR", "FR", "FR", "FR", "FR", "FR", "F…
## $ Pais          <chr> "Spain", "Spain", "France", "France", "France", "France"…
## $ Ciudad        <chr> "Barcelona", "Madrid", "Bordeaux", "Lille", "Lyon", "Mar…
## $ Poblacion     <dbl> 1620343, 3223334, 254436, 232787, 516092, 863310, 285121…
## $ DenPob        <dbl> 16014, 5332, 5104, 6687, 10758, 3564, 4997, 4704, 4586, …
## $ ArCiclista    <chr> "0.15", "0.067", "0.106", "0.098", "0.109", "0.044", "0.…
## $ ArBajaVel     <chr> "0.775", "0.065", "0.239", "0.634", "0.284", "0.118", "0…
## $ PMPeatones    <chr> "32.0", "34.0", "21.0", "32.0", "34.0", "34.0", "26.0", …
## $ PMCiclistas   <chr> "2.0", "0.46", "3.0", "2.0", "2.0", "1.0", "2.0", "5.0",…
## $ PMTPublico    <chr> "39.0", "24.35", "9.0", "10.0", "19.0", "11.0", "8.0", "…
## $ PMVMotor      <chr> "27.0", "39.89", "67.0", "56.0", "45.0", "54.0", "64.0",…
## $ PeatAuto      <dbl> 22, 201, 13, 5, 31, 56, 21, 13, 20, 58, 5, 12, 153, 60, …
## $ CicAuto       <dbl> 5, 18, 5, 1, 11, 9, 4, 7, 2, 14, 8, 2, 54, 14, 28, 15, 2…
## $ V2RMSM        <dbl> 29, 95, 2, 2, 12, 45, 8, 6, 6, 48, 4, 5, 6, 8, 1, 1, 2, …
## $ V2RMAuto      <dbl> 96, 275, 14, 7, 39, 146, 21, 13, 50, 132, 8, 29, 55, 11,…
## $ AutoSM        <dbl> 0, 44, 3, 2, 8, 30, 9, 1, 5, 12, 4, 5, 20, 18, 6, 7, 11,…
## $ AutoAuto      <dbl> 4, 72, 1, 6, 21, 48, 12, 6, 6, 24, 3, 12, 122, 31, 11, 1…
## $ Precipitacion <chr> "565.0", "420.9", "944.1", "742.5", "831.9", "515.4", "6…
## $ Temp          <chr> "18.2", "15.0", "13.8", "10.8", "12.5", "15.5", "15.1", …
## $ PBI           <dbl> 31070.56, 35290.16, 35464.54, 31291.93, 48140.02, 36231.…
describe(segvial)
##                vars  n      mean        sd    median   trimmed       mad
## Codigo Pais*      1 24      2.54      0.88      2.50      2.50      0.74
## Pais*             2 24      3.04      1.90      3.50      3.05      2.22
## Ciudad*           3 24     12.50      7.07     12.50     12.50      8.90
## Poblacion         4 24 976996.17 984150.14 542400.00 806857.40 355711.32
## DenPob            5 24   5483.00   4725.45   4212.50   4597.95   1490.75
## ArCiclista*       6 24     11.79      6.71     11.50     11.75      8.15
## ArBajaVel*        7 24     12.50      7.07     12.50     12.50      8.90
## PMPeatones*       8 24      6.67      3.87      7.00      6.55      4.45
## PMCiclistas*      9 24      3.71      1.57      4.00      3.65      1.48
## PMTPublico*      10 24      9.29      5.34      9.00      9.20      6.67
## PMVMotor*        11 24     10.79      6.33     10.50     10.75      8.15
## PeatAuto         12 24     70.71     93.08     36.50     52.00     35.58
## CicAuto          13 24     30.46     67.59     14.00     16.70     13.34
## V2RMSM           14 24     21.96     35.53      6.00     14.35      5.93
## V2RMAuto         15 24     74.83    106.46     29.50     53.50     25.95
## AutoSM           16 24     18.04     28.55      7.50     12.50      7.41
## AutoAuto         17 24     40.17     53.33     17.50     31.05     18.53
## Precipitacion*   18 24     12.50      7.07     12.50     12.50      8.90
## Temp*            19 24     10.79      6.40     10.50     10.80      8.15
## PBI              20 24  50517.45  31812.18  37086.79  44973.69   8503.38
##                      min     max     range  skew kurtosis        se
## Codigo Pais*        1.00       5       4.0  0.60     0.67      0.18
## Pais*               1.00       5       4.0 -0.06    -1.96      0.39
## Ciudad*             1.00      24      23.0  0.00    -1.35      1.44
## Poblacion      232787.00 3600203 3367416.0  1.55     0.99 200888.81
## DenPob           1417.00   20772   19355.0  1.85     2.85    964.58
## ArCiclista*         1.00      23      22.0  0.07    -1.34      1.37
## ArBajaVel*          1.00      24      23.0  0.00    -1.35      1.44
## PMPeatones*         1.00      14      13.0  0.11    -1.14      0.79
## PMCiclistas*        1.00       7       6.0  0.14    -0.98      0.32
## PMTPublico*         1.00      18      17.0  0.12    -1.31      1.09
## PMVMotor*           1.00      21      20.0  0.07    -1.45      1.29
## PeatAuto            5.00     398     393.0  2.14     4.18     19.00
## CicAuto             1.00     340     339.0  3.98    15.34     13.80
## V2RMSM              1.00     143     142.0  2.06     3.58      7.25
## V2RMAuto            2.00     371     369.0  1.78     1.84     21.73
## AutoSM              0.00     138     138.0  3.09    10.09      5.83
## AutoAuto            1.00     217     216.0  1.81     2.73     10.89
## Precipitacion*      1.00      24      23.0  0.00    -1.35      1.44
## Temp*               1.00      21      20.0  0.01    -1.39      1.31
## PBI             23867.44  152178  128310.6  1.83     2.40   6493.63
summary = summary(segvial)
summary
##  Codigo Pais            Pais              Ciudad            Poblacion      
##  Length:24          Length:24          Length:24          Min.   : 232787  
##  Class :character   Class :character   Class :character   1st Qu.: 432558  
##  Mode  :character   Mode  :character   Mode  :character   Median : 542400  
##                                                           Mean   : 976996  
##                                                           3rd Qu.: 932826  
##                                                           Max.   :3600203  
##      DenPob       ArCiclista         ArBajaVel          PMPeatones       
##  Min.   : 1417   Length:24          Length:24          Length:24         
##  1st Qu.: 3223   Class :character   Class :character   Class :character  
##  Median : 4212   Mode  :character   Mode  :character   Mode  :character  
##  Mean   : 5483                                                           
##  3rd Qu.: 5161                                                           
##  Max.   :20772                                                           
##  PMCiclistas         PMTPublico          PMVMotor            PeatAuto     
##  Length:24          Length:24          Length:24          Min.   :  5.00  
##  Class :character   Class :character   Class :character   1st Qu.: 18.25  
##  Mode  :character   Mode  :character   Mode  :character   Median : 36.50  
##                                                           Mean   : 70.71  
##                                                           3rd Qu.: 67.25  
##                                                           Max.   :398.00  
##     CicAuto           V2RMSM          V2RMAuto          AutoSM      
##  Min.   :  1.00   Min.   :  1.00   Min.   :  2.00   Min.   :  0.00  
##  1st Qu.:  5.75   1st Qu.:  3.50   1st Qu.: 15.50   1st Qu.:  4.75  
##  Median : 14.00   Median :  6.00   Median : 29.50   Median :  7.50  
##  Mean   : 30.46   Mean   : 21.96   Mean   : 74.83   Mean   : 18.04  
##  3rd Qu.: 27.25   3rd Qu.: 20.00   3rd Qu.: 65.25   3rd Qu.: 21.00  
##  Max.   :340.00   Max.   :143.00   Max.   :371.00   Max.   :138.00  
##     AutoAuto      Precipitacion          Temp                PBI        
##  Min.   :  1.00   Length:24          Length:24          Min.   : 23867  
##  1st Qu.:  6.00   Class :character   Class :character   1st Qu.: 34399  
##  Median : 17.50   Mode  :character   Mode  :character   Median : 37087  
##  Mean   : 40.17                                         Mean   : 50517  
##  3rd Qu.: 49.25                                         3rd Qu.: 48993  
##  Max.   :217.00                                         Max.   :152178

Contamos con 20 columnas y 24 filas,de las cuales 3 son categoricas y el resto numericas, sin embargo figuran como de tipo varchar, por lo que las trasnformaremos a numericas. Los índices presentan una gran difernecia en cuanto a magnitud entre su media, minimo y máximo. El resto de las variables pareciera tener distirbuciones similares.

segvial <- transform(segvial,
                     ArCiclista = as.numeric(ArCiclista),
                     ArBajaVel = as.numeric(ArBajaVel),
                     PMPeatones = as.numeric(PMPeatones),
                     PMCiclistas = as.numeric(PMCiclistas),
                     PMTPublico = as.numeric(PMTPublico),
                     PMVMotor = as.numeric(PMVMotor),
                     Precipitacion = as.numeric(Precipitacion),
                     Temp = as.numeric(Temp))
segvial
##    Codigo.Pais           Pais       Ciudad Poblacion DenPob ArCiclista
## 1           ES          Spain    Barcelona   1620343  16014      0.150
## 2           ES          Spain       Madrid   3223334   5332      0.067
## 3           FR         France     Bordeaux    254436   5104      0.106
## 4           FR         France        Lille    232787   6687      0.098
## 5           FR         France         Lyon    516092  10758      0.109
## 6           FR         France    Marseille    863310   3564      0.044
## 7           FR         France  Montpellier    285121   4997      0.167
## 8           FR         France       Nantes    309346   4704      0.327
## 9           FR         France         Nice    340017   4586      0.056
## 10          FR         France        Paris   2187526  20772      0.186
## 11          FR         France   Strasbourg    280966   3591      0.333
## 12          FR         France     Toulouse    479553   4064      0.211
## 13          GB United Kingdom   Birmingham   1141374   4262      0.074
## 14          GB United Kingdom     Bradford    537173   1465      0.030
## 15          GB United Kingdom      Bristol    463405   4163      0.107
## 16          GB United Kingdom    Edinburgh    518500   1899      0.119
## 17          GB United Kingdom      Glasgow    626410   3553      0.067
## 18          GB United Kingdom        Leeds    789194   1430      0.085
## 19          GB United Kingdom    Liverpool    494814   3704      0.033
## 20          GB United Kingdom Inner London   3600203  10975      0.108
## 21          GB United Kingdom   Manchester    547627   4735      0.082
## 22          GB United Kingdom    Sheffield    582506   1583      0.053
## 23          IT          Italy         Rome   2872800   2233      0.027
## 24          NO         Norway         Oslo    681071   1417      0.163
##    ArBajaVel PMPeatones PMCiclistas PMTPublico PMVMotor PeatAuto CicAuto V2RMSM
## 1      0.775       32.0        2.00      39.00    27.00       22       5     29
## 2      0.065       34.0        0.46      24.35    39.89      201      18     95
## 3      0.239       21.0        3.00       9.00    67.00       13       5      2
## 4      0.634       32.0        2.00      10.00    56.00        5       1      2
## 5      0.284       34.0        2.00      19.00    45.00       31      11     12
## 6      0.118       34.0        1.00      11.00    54.00       56       9     45
## 7      0.125       26.0        2.00       8.00    64.00       21       4      8
## 8      0.354       27.0        5.00      16.00    52.00       13       7      6
## 9      0.102       40.0        1.00      11.00    48.00       20       2      6
## 10     0.491       47.0        3.00      33.00    17.00       58      14     48
## 11     0.187       33.0        8.00      12.00    47.00        5       8      4
## 12     0.385       21.0        3.00       9.00    67.00       12       2      5
## 13     0.186       10.0        2.00      18.00    70.00      153      54      6
## 14     0.024        5.0        1.00      31.00    63.00       60      14      8
## 15     0.460       21.0       14.00      16.00    49.00       25      28      1
## 16     0.745       34.0        2.00      19.00    45.00       33      15      1
## 17     0.095       27.0        1.00      33.00    39.00       61      23      2
## 18     0.058        4.0        1.00      38.00    57.00       75      50     17
## 19     0.242       12.0        2.00      26.00    61.00       71      27      5
## 20     0.871       25.0        3.00      36.00    37.00      398     340     72
## 21     0.393       10.0        2.00      17.00    71.00       40      34      4
## 22     0.039       10.0        1.00      34.00    55.00       66      39      4
## 23     0.029        4.6        1.00      28.90    65.50      249      15    143
## 24     0.237       32.0        5.00      26.00    37.00        9       6      2
##    V2RMAuto AutoSM AutoAuto Precipitacion Temp       PBI
## 1        96      0        4         565.0 18.2  31070.56
## 2       275     44       72         420.9 15.0  35290.16
## 3        14      3        1         944.1 13.8  35464.54
## 4         7      2        6         742.5 10.8  31291.93
## 5        39      8       21         831.9 12.5  48140.02
## 6       146     30       48         515.4 15.5  36231.61
## 7        21      9       12         629.1 15.1 152178.01
## 8        13      1        6         819.5 12.5  36188.05
## 9        50      5        6         733.0 16.0  36217.14
## 10      132     12       24         637.4 12.4 109123.01
## 11        8      4        3         665.0 10.9  34824.27
## 12       29      5       12         638.0 14.3  42762.83
## 13       55     20      122         804.9  9.7  29764.20
## 14       11     18       31         872.8  9.4  23867.44
## 15       22      6       11         802.1 10.6  39810.18
## 16       17      7       14         704.3  9.3  55206.04
## 17       16     11       14        1245.3  9.6  42480.23
## 18       43     24       53         809.9  9.4  41428.04
## 19       22      7       22         836.6 10.1  33121.24
## 20      371     44      123         601.7 11.3 101968.91
## 21       31      5       28         828.8 10.0  51550.57
## 22       30     28      112         834.6 10.0  27776.48
## 23      346    138      217         798.5 15.3  37941.96
## 24        2      2        2         802.7  6.8  98721.31

Categorización de variables

Las variables pueden ser categorizadas en cuatro categorías:

  • Métrica contempla todos los inidices MDS

  • Ambientales consideramos temas de ambiente como la tempratura y las lluvias

  • Demográficas es relativo a población

  • Geográfica son las que representan país o ciudad

  • Características urbanas son aquellas relativas a áreas o espacio de los diferentes agentes viales

Curiosidades

El objetivo es encontrar vulnerabilidad o ciertas características que nos permitan tomar medidas de seguridad vial, por lo que surgen las siguientes inquietudes que buscaremos resolver:

  • En aquellas ciudades que llueve más ¿se generan mayor cantidad de accidentes tanto por si mismo como entre otros?

  • Las ciudades que tienen menos espacio para ciclistas presentan mayores accidentes

  • Si la temperatura promedio es agradable hay mayores accidentes en bicicleta o peaton

  • Existe alguna relacion con el área o participación y sus accidentes

  • Aquellas ciudades que tienen mayores zonas de baja velocidad que indices presentan

Buscaremos dar respuesta a todas estas inquietudes de manera gráfica.

Análisis gráfico

Lluvia vs MSD

ggplot(segvial, aes(x = PeatAuto, y = Precipitacion, color = Precipitacion)) +
  geom_point()

ggplot(segvial, aes(x = CicAuto, y = Precipitacion, color = Precipitacion)) +
  geom_point()

ggplot(segvial, aes(x = V2RMSM, y = Precipitacion, color = Precipitacion)) +
  geom_point()

ggplot(segvial, aes(x = V2RMAuto, y = Precipitacion, color = Precipitacion)) +
  geom_point()

ggplot(segvial, aes(x = AutoSM, y = Precipitacion, color = Precipitacion)) +
  geom_point()

ggplot(segvial, aes(x = AutoAuto, y = Precipitacion, color = Precipitacion)) +
  geom_point()

No pareciera que aquellos lugares donde más llueve presenten los mayores indices.

Ciudades vs espacio ciclista

ggplot(segvial, aes(x = CicAuto, y = ArCiclista, color = Pais)) +
  geom_point()

ggplot(segvial, aes(x = CicAuto, y = PMCiclistas, color = Pais)) +
  geom_point()

No pareciera que se puedan identificar clusters particulares de un único país si podemos observar que hay un valor para el indice CicAuto de reino unido cuya magnitud esta muy por encima del resto

Temparatura vs Accidentes bicicleta o peaton

ggplot(segvial, aes(x = Temp, y = CicAuto)) +
  geom_point()

ggplot(segvial, aes(x = Temp, y = PeatAuto)) +
  geom_point()

Pareciera que si la temperatura es agradable la mayor cantidad del año se produzcan más accidentes en bicicleta o peatonal, lo cual hae sentido si pensamos que la gente saldría más de su hogar a realizar alguna actividad.

Participacion vs indice

ggplot(segvial, aes(x = PeatAuto, y = PMVMotor, color = PMVMotor)) +
  geom_point()

ggplot(segvial, aes(x = CicAuto, y = PMCiclistas, color = PMCiclistas)) +
  geom_point()

ggplot(segvial, aes(x = V2RMSM, y = PMVMotor, color = PMVMotor)) +
  geom_point()

ggplot(segvial, aes(x = V2RMAuto, y = PMVMotor, color = PMVMotor)) +
  geom_point()

ggplot(segvial, aes(x = AutoSM, y = PMVMotor, color = PMVMotor)) +
  geom_point()

ggplot(segvial, aes(x = AutoAuto, y = PMVMotor, color = PMVMotor)) +
  geom_point()

La participación que tiene el espacio si se trata de un vehiculo, peaton o ciclista pareciera que tiene cierta inferencia en los indices de accidentes, donde en algunos casos se puede ver que a mayor cantidad es más grande el indice.

Zona de baja velocidad vs indice

ggplot(segvial, aes(x = PeatAuto, y = ArBajaVel, color = ArBajaVel)) +
  geom_point()

ggplot(segvial, aes(x = CicAuto, y = ArBajaVel, color = ArBajaVel)) +
  geom_point()

ggplot(segvial, aes(x = V2RMSM, y = ArBajaVel, color = ArBajaVel)) +
  geom_point()

ggplot(segvial, aes(x = V2RMAuto, y = ArBajaVel, color = ArBajaVel)) +
  geom_point()

ggplot(segvial, aes(x = AutoSM, y = ArBajaVel, color = ArBajaVel)) +
  geom_point()

ggplot(segvial, aes(x = AutoAuto, y = ArBajaVel, color = Precipitacion)) +
  geom_point()

Se puede observar principalmente en quellos graficos que muestran indices de vehículos que hay mayores indices para algunas ciudades, también que en aquellas ciudades que tienen mayores porcentajes de áreas destinadas de baja velocidad presentan menores indicaroes para peaton y ciclista.

Histogramas

Utilizamos histogramas para ver la distribución de las variables de respuesta.

# PeatAuto CicAuto V2RMSM V2RMAut AutoSM AutoAuto
hp <- segvial %>%
  ggplot( aes(x=PeatAuto)) +
    geom_histogram( binwidth=3, fill="#69b3a2", color="#e9ecef", alpha=0.9) +
    ggtitle("Distribucion de PeatAuto") +
    theme_ipsum() +
    theme(
      plot.title = element_text(size=8)
    )


hc <- segvial %>%
  ggplot( aes(x=CicAuto)) +
    geom_histogram( binwidth=3, fill="#9a76db", color="#e9ecef", alpha=0.9) +
    ggtitle("Distribucion de CicAuto") +
    theme_ipsum() +
    theme(
      plot.title = element_text(size=8)
    )


hv2rm <- segvial %>%
  ggplot( aes(x=V2RMSM)) +
    geom_histogram( binwidth=3, fill="#60bd88", color="#e9ecef", alpha=0.9) +
    ggtitle("Distribucion del V2RMSM") +
    theme_ipsum() +
    theme(
      plot.title = element_text(size=8)
    )

hv2a <- segvial %>%
  ggplot( aes(x=V2RMAuto)) +
    geom_histogram( binwidth=3, fill="#609ebd", color="#e9ecef", alpha=0.9) +
    ggtitle("Distribucion de V2RMAuto") +
    theme_ipsum() +
    theme(
      plot.title = element_text(size=8)
    )

has <- segvial %>%
  ggplot( aes(x=AutoSM)) +
    geom_histogram( binwidth=3, fill="#609ebd", color="#e9ecef", alpha=0.9) +
    ggtitle("Distribucion de AutoSM") +
    theme_ipsum() +
    theme(
      plot.title = element_text(size=8)
    )

haa <- segvial %>%
  ggplot( aes(x=AutoAuto)) +
    geom_histogram( binwidth=3, fill="#609ebd", color="#e9ecef", alpha=0.9) +
    ggtitle("Distribucion de AutoAuto") +
    theme_ipsum() +
    theme(
      plot.title = element_text(size=8)
    )

ggarrange(hp,hc,hv2rm,hv2a,has,haa,
          nrow = 1,
          ncol = 2
          )
## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database

## Warning in grid.Call(C_stringMetric, as.graphicsAnnot(x$label)): font family
## 'Arial Narrow' not found in PostScript font database
## $`1`

## 
## $`2`

## 
## $`3`

## 
## attr(,"class")
## [1] "list"      "ggarrange"

Correlación General

Vamos a realizar una matriz de correlación general considerando todas las variables

ggcorr(segvial, method = c("everything", "pearson"))
## Warning in ggcorr(segvial, method = c("everything", "pearson")): data in
## column(s) 'Codigo.Pais', 'Pais', 'Ciudad' are not numeric and were ignored

Existen varias correlaciones fuertes que podrían estar indicando posible colieanidad.

Estandarización de variables

Variables respuesta
segvial$stPeatAuto <- PeatAuto/Poblacion*(10^6)
segvial$stCicAuto <- CicAuto/Poblacion*(10^6)
segvial$stV2RMSM <- V2RMSM/Poblacion*(10^6)
segvial$stV2RMAuto <- V2RMAuto/Poblacion*(10^6)
segvial$stAutoSM <- AutoSM/Poblacion*(10^6)
segvial$stAutoAuto <- AutoAuto/Poblacion*(10^6)
segvial
##    Codigo.Pais           Pais       Ciudad Poblacion DenPob ArCiclista
## 1           ES          Spain    Barcelona   1620343  16014      0.150
## 2           ES          Spain       Madrid   3223334   5332      0.067
## 3           FR         France     Bordeaux    254436   5104      0.106
## 4           FR         France        Lille    232787   6687      0.098
## 5           FR         France         Lyon    516092  10758      0.109
## 6           FR         France    Marseille    863310   3564      0.044
## 7           FR         France  Montpellier    285121   4997      0.167
## 8           FR         France       Nantes    309346   4704      0.327
## 9           FR         France         Nice    340017   4586      0.056
## 10          FR         France        Paris   2187526  20772      0.186
## 11          FR         France   Strasbourg    280966   3591      0.333
## 12          FR         France     Toulouse    479553   4064      0.211
## 13          GB United Kingdom   Birmingham   1141374   4262      0.074
## 14          GB United Kingdom     Bradford    537173   1465      0.030
## 15          GB United Kingdom      Bristol    463405   4163      0.107
## 16          GB United Kingdom    Edinburgh    518500   1899      0.119
## 17          GB United Kingdom      Glasgow    626410   3553      0.067
## 18          GB United Kingdom        Leeds    789194   1430      0.085
## 19          GB United Kingdom    Liverpool    494814   3704      0.033
## 20          GB United Kingdom Inner London   3600203  10975      0.108
## 21          GB United Kingdom   Manchester    547627   4735      0.082
## 22          GB United Kingdom    Sheffield    582506   1583      0.053
## 23          IT          Italy         Rome   2872800   2233      0.027
## 24          NO         Norway         Oslo    681071   1417      0.163
##    ArBajaVel PMPeatones PMCiclistas PMTPublico PMVMotor PeatAuto CicAuto V2RMSM
## 1      0.775       32.0        2.00      39.00    27.00       22       5     29
## 2      0.065       34.0        0.46      24.35    39.89      201      18     95
## 3      0.239       21.0        3.00       9.00    67.00       13       5      2
## 4      0.634       32.0        2.00      10.00    56.00        5       1      2
## 5      0.284       34.0        2.00      19.00    45.00       31      11     12
## 6      0.118       34.0        1.00      11.00    54.00       56       9     45
## 7      0.125       26.0        2.00       8.00    64.00       21       4      8
## 8      0.354       27.0        5.00      16.00    52.00       13       7      6
## 9      0.102       40.0        1.00      11.00    48.00       20       2      6
## 10     0.491       47.0        3.00      33.00    17.00       58      14     48
## 11     0.187       33.0        8.00      12.00    47.00        5       8      4
## 12     0.385       21.0        3.00       9.00    67.00       12       2      5
## 13     0.186       10.0        2.00      18.00    70.00      153      54      6
## 14     0.024        5.0        1.00      31.00    63.00       60      14      8
## 15     0.460       21.0       14.00      16.00    49.00       25      28      1
## 16     0.745       34.0        2.00      19.00    45.00       33      15      1
## 17     0.095       27.0        1.00      33.00    39.00       61      23      2
## 18     0.058        4.0        1.00      38.00    57.00       75      50     17
## 19     0.242       12.0        2.00      26.00    61.00       71      27      5
## 20     0.871       25.0        3.00      36.00    37.00      398     340     72
## 21     0.393       10.0        2.00      17.00    71.00       40      34      4
## 22     0.039       10.0        1.00      34.00    55.00       66      39      4
## 23     0.029        4.6        1.00      28.90    65.50      249      15    143
## 24     0.237       32.0        5.00      26.00    37.00        9       6      2
##    V2RMAuto AutoSM AutoAuto Precipitacion Temp       PBI stPeatAuto stCicAuto
## 1        96      0        4         565.0 18.2  31070.56   13.57737  3.085766
## 2       275     44       72         420.9 15.0  35290.16   62.35779  5.584280
## 3        14      3        1         944.1 13.8  35464.54   51.09340 19.651307
## 4         7      2        6         742.5 10.8  31291.93   21.47886  4.295773
## 5        39      8       21         831.9 12.5  48140.02   60.06681 21.314029
## 6       146     30       48         515.4 15.5  36231.61   64.86662 10.424992
## 7        21      9       12         629.1 15.1 152178.01   73.65294 14.029131
## 8        13      1        6         819.5 12.5  36188.05   42.02414 22.628384
## 9        50      5        6         733.0 16.0  36217.14   58.82059  5.882059
## 10      132     12       24         637.4 12.4 109123.01   26.51397  6.399924
## 11        8      4        3         665.0 10.9  34824.27   17.79575 28.473196
## 12       29      5       12         638.0 14.3  42762.83   25.02330  4.170550
## 13       55     20      122         804.9  9.7  29764.20  134.04896 47.311398
## 14       11     18       31         872.8  9.4  23867.44  111.69586 26.062367
## 15       22      6       11         802.1 10.6  39810.18   53.94849 60.422309
## 16       17      7       14         704.3  9.3  55206.04   63.64513 28.929605
## 17       16     11       14        1245.3  9.6  42480.23   97.38031 36.717166
## 18       43     24       53         809.9  9.4  41428.04   95.03367 63.355778
## 19       22      7       22         836.6 10.1  33121.24  143.48826 54.565958
## 20      371     44      123         601.7 11.3 101968.91  110.54932 94.439119
## 21       31      5       28         828.8 10.0  51550.57   73.04242 62.086055
## 22       30     28      112         834.6 10.0  27776.48  113.30355 66.952100
## 23      346    138      217         798.5 15.3  37941.96   86.67502  5.221387
## 24        2      2        2         802.7  6.8  98721.31   13.21448  8.809654
##     stV2RMSM stV2RMAuto  stAutoSM stAutoAuto
## 1  17.897445  59.246715  0.000000   2.468613
## 2  29.472590  85.315391 13.650463  22.337121
## 3   7.860523  55.023660 11.790784   3.930261
## 4   8.591545  30.070408  8.591545  25.774635
## 5  23.251668  75.567922 15.501112  40.690420
## 6  52.124961 169.116540 34.749974  55.599958
## 7  28.058263  73.652940 31.565546  42.087394
## 8  19.395758  42.024141  3.232626  19.395758
## 9  17.646177 147.051471 14.705147  17.646177
## 10 21.942596  60.342140  5.485649  10.971298
## 11 14.236598  28.473196 14.236598  10.677449
## 12 10.426376  60.472982 10.426376  25.023303
## 13  5.256822  48.187535 17.522740 106.888715
## 14 14.892781  20.477574 33.508758  57.709527
## 15  2.157940  47.474671 12.947638  23.737336
## 16  1.928640  32.786885 13.500482  27.000964
## 17  3.192797  25.542376 17.560384  22.349579
## 18 21.540965  54.485969 30.410774  67.157125
## 19 10.104807  44.461151 14.146730  44.461151
## 20 19.998872 103.049745 12.221533  34.164740
## 21  7.304242  56.607874  9.130302  51.129692
## 22  6.866882  51.501615 48.068174 192.272698
## 23 49.777221 120.439989 48.036759  75.536062
## 24  2.936551   2.936551  2.936551   2.936551
Variables regresoras
  • DenPob,ArCiclista,ArBajaVel,PMPeatones,PMCiclistas,PMTPublico,PMVMotor,Temp,Precipitacion,PBI
df_segvial <- segvial%>% mutate_at (c ('Precipitacion', 'PBI', 'Temp','PMVMotor','PMTPublico','PMCiclistas','PMPeatones','ArBajaVel','ArCiclista','DenPob'), ~ ( scale (.)%>% as.vector ))
df_segvial
##    Codigo.Pais           Pais       Ciudad Poblacion      DenPob  ArCiclista
## 1           ES          Spain    Barcelona   1620343  2.22857298  0.40404755
## 2           ES          Spain       Madrid   3223334 -0.03195466 -0.60455235
## 3           FR         France     Bordeaux    254436 -0.08020408 -0.13063191
## 4           FR         France        Lille    232787  0.25479080 -0.22784636
## 5           FR         France         Lyon    516092  1.11629688 -0.09417650
## 6           FR         France    Marseille    863310 -0.40609928 -0.88404389
## 7           FR         France  Montpellier    285121 -0.10284745  0.61062825
## 8           FR         France       Nantes    309346 -0.16485218  2.55491722
## 9           FR         France         Nice    340017 -0.18982338 -0.73822222
## 10          FR         France        Paris   2187526  3.23546219  0.84151257
## 11          FR         France   Strasbourg    280966 -0.40038554  2.62782805
## 12          FR         France     Toulouse    479553 -0.30028915  1.14530772
## 13          GB United Kingdom   Birmingham   1141374 -0.25838834 -0.51948971
## 14          GB United Kingdom     Bradford    537173 -0.85029021 -1.05416917
## 15          GB United Kingdom      Bristol    463405 -0.27933875 -0.11848011
## 16          GB United Kingdom    Edinburgh    518500 -0.75844702  0.02734156
## 17          GB United Kingdom      Glasgow    626410 -0.40842711 -0.60455235
## 18          GB United Kingdom        Leeds    789194 -0.85769692 -0.38581984
## 19          GB United Kingdom    Liverpool    494814 -0.37647245 -1.01771375
## 20          GB United Kingdom Inner London   3600203  1.16221848 -0.10632830
## 21          GB United Kingdom   Manchester    547627 -0.15829196 -0.42227526
## 22          GB United Kingdom    Sheffield    582506 -0.82531902 -0.77467763
## 23          IT          Italy         Rome   2872800 -0.68776585 -1.09062459
## 24          NO         Norway         Oslo    681071 -0.86044799  0.56202103
##     ArBajaVel  PMPeatones PMCiclistas PMTPublico    PMVMotor PeatAuto CicAuto
## 1   1.8984595  0.66156039 -0.27804757  1.6644700 -1.74922903       22       5
## 2  -0.9238882  0.82660664 -0.80613791  0.2431521 -0.82481711      201      18
## 3  -0.2322143 -0.24619399  0.06486824 -1.2460786  1.11938825       13       5
## 4   1.3379651  0.66156039 -0.27804757 -1.1490604  0.33051850        5       1
## 5  -0.0533331  0.82660664 -0.27804757 -0.2758958 -0.45835126       31      11
## 6  -0.7132059  0.82660664 -0.62096337 -1.0520421  0.18708763       56       9
## 7  -0.6853800  0.16642164 -0.27804757 -1.3430969  0.90424195       21       4
## 8   0.2249265  0.24894476  0.75069985 -0.5669506  0.04365677       13       7
## 9  -0.7768081  1.32174540 -0.62096337 -1.0520421 -0.24320496       20       2
## 10  0.7695204  1.89940728  0.06486824  1.0823603 -2.46638335       58      14
## 11 -0.4389214  0.74408352  1.77944727 -0.9550238 -0.31492039        5       8
## 12  0.3481558 -0.24619399  0.06486824 -1.2460786  1.11938825       12       2
## 13 -0.4428966 -1.15394837 -0.27804757 -0.3729140  1.33453455      153      54
## 14 -1.0868689 -1.56656400 -0.62096337  0.8883237  0.83252652       60      14
## 15  0.6462911 -0.24619399  3.83694212 -0.5669506 -0.17148953       25      28
## 16  1.7792053  0.82660664 -0.27804757 -0.2758958 -0.45835126       33      15
## 17 -0.8046341  0.24894476 -0.62096337  1.0823603 -0.88864385       61      23
## 18 -0.9517142 -1.64908713 -0.62096337  1.5674517  0.40223393       75      50
## 19 -0.2202889 -0.98890212 -0.27804757  0.4032323  0.68909566       71      27
## 20  2.2800727  0.08389851  0.06486824  1.3734151 -1.03207471      398     340
## 21  0.3799569 -1.15394837 -0.27804757 -0.4699323  1.40624998       40      34
## 22 -1.0272418 -1.15394837 -0.62096337  1.1793786  0.25880307       66      39
## 23 -1.0669932 -1.59957325 -0.62096337  0.6845853  1.01181510      249      15
## 24 -0.2401646  0.66156039  0.75069985  0.4032323 -1.03207471        9       6
##    V2RMSM V2RMAuto AutoSM AutoAuto Precipitacion       Temp         PBI
## 1      29       96      0        4   -1.15398383  2.2028201 -0.61130324
## 2      95      275     44       72   -2.03615396  1.0620475 -0.47866210
## 3       2       14      3        1    1.16683988  0.6342577 -0.47318053
## 4       2        7      2        6   -0.06734123 -0.4352167 -0.60434454
## 5      12       39      8       21    0.47995932  0.1708188 -0.07473309
## 6      45      146     30       48   -1.45763156  1.2402932 -0.44906814
## 7       8       21      9       12   -0.76156811  1.0976966  3.19564902
## 8       6       13      1        6    0.40404739  0.1708188 -0.45043750
## 9       6       50      5        6   -0.12549957  1.4185389 -0.44952287
## 10     48      132     12       24   -0.71075609  0.1351697  1.84223680
## 11      4        8      4        3   -0.54179082 -0.3995675 -0.49330720
## 12      5       29      5       12   -0.70708293  0.8125034 -0.24376255
## 13      6       55     20      122    0.31466721 -0.8273573 -0.65236796
## 14      8       11     18       31    0.73034626 -0.9343047 -0.83772977
## 15      1       22      6       11    0.29752580 -0.5065149 -0.33657759
## 16      1       17      7       14   -0.30119896 -0.9699538  0.14738356
## 17      2       16     11       14    3.01076524 -0.8630064 -0.25264595
## 18     17       43     24       53    0.34527686 -0.9343047 -0.28572111
## 19      5       22      7       22    0.50873239 -0.6847607 -0.54684114
## 20     72      371     44      123   -0.92930899 -0.2569709  1.61735105
## 21      4       31      5       28    0.46098134 -0.7204098  0.03247566
## 22      4       30     28      112    0.49648853 -0.7204098 -0.71485101
## 23    143      346    138      217    0.27548686  1.1689949 -0.39530410
## 24      2        2      2        2    0.30119896 -1.8611825  1.51526431
##    stPeatAuto stCicAuto  stV2RMSM stV2RMAuto  stAutoSM stAutoAuto
## 1    13.57737  3.085766 17.897445  59.246715  0.000000   2.468613
## 2    62.35779  5.584280 29.472590  85.315391 13.650463  22.337121
## 3    51.09340 19.651307  7.860523  55.023660 11.790784   3.930261
## 4    21.47886  4.295773  8.591545  30.070408  8.591545  25.774635
## 5    60.06681 21.314029 23.251668  75.567922 15.501112  40.690420
## 6    64.86662 10.424992 52.124961 169.116540 34.749974  55.599958
## 7    73.65294 14.029131 28.058263  73.652940 31.565546  42.087394
## 8    42.02414 22.628384 19.395758  42.024141  3.232626  19.395758
## 9    58.82059  5.882059 17.646177 147.051471 14.705147  17.646177
## 10   26.51397  6.399924 21.942596  60.342140  5.485649  10.971298
## 11   17.79575 28.473196 14.236598  28.473196 14.236598  10.677449
## 12   25.02330  4.170550 10.426376  60.472982 10.426376  25.023303
## 13  134.04896 47.311398  5.256822  48.187535 17.522740 106.888715
## 14  111.69586 26.062367 14.892781  20.477574 33.508758  57.709527
## 15   53.94849 60.422309  2.157940  47.474671 12.947638  23.737336
## 16   63.64513 28.929605  1.928640  32.786885 13.500482  27.000964
## 17   97.38031 36.717166  3.192797  25.542376 17.560384  22.349579
## 18   95.03367 63.355778 21.540965  54.485969 30.410774  67.157125
## 19  143.48826 54.565958 10.104807  44.461151 14.146730  44.461151
## 20  110.54932 94.439119 19.998872 103.049745 12.221533  34.164740
## 21   73.04242 62.086055  7.304242  56.607874  9.130302  51.129692
## 22  113.30355 66.952100  6.866882  51.501615 48.068174 192.272698
## 23   86.67502  5.221387 49.777221 120.439989 48.036759  75.536062
## 24   13.21448  8.809654  2.936551   2.936551  2.936551   2.936551

Modelado

Datos

Para poder evaluar la capacidad predictiva de cada modelo, se dividen las observaciones disponibles en dos grupos: uno de entrenamiento y otro de test, para cada variable de respuesta.

PeatAuto

split <- sample.split(df_segvial$stPeatAuto, SplitRatio = 2/3)
training_set_pa <- subset(df_segvial, split==T)
test_set_pa    <- subset(df_segvial, split==F)
training_set_pa
##    Codigo.Pais           Pais     Ciudad Poblacion      DenPob  ArCiclista
## 2           ES          Spain     Madrid   3223334 -0.03195466 -0.60455235
## 3           FR         France   Bordeaux    254436 -0.08020408 -0.13063191
## 4           FR         France      Lille    232787  0.25479080 -0.22784636
## 5           FR         France       Lyon    516092  1.11629688 -0.09417650
## 6           FR         France  Marseille    863310 -0.40609928 -0.88404389
## 9           FR         France       Nice    340017 -0.18982338 -0.73822222
## 10          FR         France      Paris   2187526  3.23546219  0.84151257
## 13          GB United Kingdom Birmingham   1141374 -0.25838834 -0.51948971
## 14          GB United Kingdom   Bradford    537173 -0.85029021 -1.05416917
## 15          GB United Kingdom    Bristol    463405 -0.27933875 -0.11848011
## 16          GB United Kingdom  Edinburgh    518500 -0.75844702  0.02734156
## 17          GB United Kingdom    Glasgow    626410 -0.40842711 -0.60455235
## 18          GB United Kingdom      Leeds    789194 -0.85769692 -0.38581984
## 21          GB United Kingdom Manchester    547627 -0.15829196 -0.42227526
## 22          GB United Kingdom  Sheffield    582506 -0.82531902 -0.77467763
## 24          NO         Norway       Oslo    681071 -0.86044799  0.56202103
##     ArBajaVel PMPeatones PMCiclistas PMTPublico   PMVMotor PeatAuto CicAuto
## 2  -0.9238882  0.8266066 -0.80613791  0.2431521 -0.8248171      201      18
## 3  -0.2322143 -0.2461940  0.06486824 -1.2460786  1.1193883       13       5
## 4   1.3379651  0.6615604 -0.27804757 -1.1490604  0.3305185        5       1
## 5  -0.0533331  0.8266066 -0.27804757 -0.2758958 -0.4583513       31      11
## 6  -0.7132059  0.8266066 -0.62096337 -1.0520421  0.1870876       56       9
## 9  -0.7768081  1.3217454 -0.62096337 -1.0520421 -0.2432050       20       2
## 10  0.7695204  1.8994073  0.06486824  1.0823603 -2.4663834       58      14
## 13 -0.4428966 -1.1539484 -0.27804757 -0.3729140  1.3345345      153      54
## 14 -1.0868689 -1.5665640 -0.62096337  0.8883237  0.8325265       60      14
## 15  0.6462911 -0.2461940  3.83694212 -0.5669506 -0.1714895       25      28
## 16  1.7792053  0.8266066 -0.27804757 -0.2758958 -0.4583513       33      15
## 17 -0.8046341  0.2489448 -0.62096337  1.0823603 -0.8886438       61      23
## 18 -0.9517142 -1.6490871 -0.62096337  1.5674517  0.4022339       75      50
## 21  0.3799569 -1.1539484 -0.27804757 -0.4699323  1.4062500       40      34
## 22 -1.0272418 -1.1539484 -0.62096337  1.1793786  0.2588031       66      39
## 24 -0.2401646  0.6615604  0.75069985  0.4032323 -1.0320747        9       6
##    V2RMSM V2RMAuto AutoSM AutoAuto Precipitacion       Temp         PBI
## 2      95      275     44       72   -2.03615396  1.0620475 -0.47866210
## 3       2       14      3        1    1.16683988  0.6342577 -0.47318053
## 4       2        7      2        6   -0.06734123 -0.4352167 -0.60434454
## 5      12       39      8       21    0.47995932  0.1708188 -0.07473309
## 6      45      146     30       48   -1.45763156  1.2402932 -0.44906814
## 9       6       50      5        6   -0.12549957  1.4185389 -0.44952287
## 10     48      132     12       24   -0.71075609  0.1351697  1.84223680
## 13      6       55     20      122    0.31466721 -0.8273573 -0.65236796
## 14      8       11     18       31    0.73034626 -0.9343047 -0.83772977
## 15      1       22      6       11    0.29752580 -0.5065149 -0.33657759
## 16      1       17      7       14   -0.30119896 -0.9699538  0.14738356
## 17      2       16     11       14    3.01076524 -0.8630064 -0.25264595
## 18     17       43     24       53    0.34527686 -0.9343047 -0.28572111
## 21      4       31      5       28    0.46098134 -0.7204098  0.03247566
## 22      4       30     28      112    0.49648853 -0.7204098 -0.71485101
## 24      2        2      2        2    0.30119896 -1.8611825  1.51526431
##    stPeatAuto stCicAuto  stV2RMSM stV2RMAuto  stAutoSM stAutoAuto
## 2    62.35779  5.584280 29.472590  85.315391 13.650463  22.337121
## 3    51.09340 19.651307  7.860523  55.023660 11.790784   3.930261
## 4    21.47886  4.295773  8.591545  30.070408  8.591545  25.774635
## 5    60.06681 21.314029 23.251668  75.567922 15.501112  40.690420
## 6    64.86662 10.424992 52.124961 169.116540 34.749974  55.599958
## 9    58.82059  5.882059 17.646177 147.051471 14.705147  17.646177
## 10   26.51397  6.399924 21.942596  60.342140  5.485649  10.971298
## 13  134.04896 47.311398  5.256822  48.187535 17.522740 106.888715
## 14  111.69586 26.062367 14.892781  20.477574 33.508758  57.709527
## 15   53.94849 60.422309  2.157940  47.474671 12.947638  23.737336
## 16   63.64513 28.929605  1.928640  32.786885 13.500482  27.000964
## 17   97.38031 36.717166  3.192797  25.542376 17.560384  22.349579
## 18   95.03367 63.355778 21.540965  54.485969 30.410774  67.157125
## 21   73.04242 62.086055  7.304242  56.607874  9.130302  51.129692
## 22  113.30355 66.952100  6.866882  51.501615 48.068174 192.272698
## 24   13.21448  8.809654  2.936551   2.936551  2.936551   2.936551

CicAuto

split <- sample.split(df_segvial$stCicAuto, SplitRatio = 2/3)
training_set_ca <- subset(df_segvial, split==T)
test_set_ca    <- subset(df_segvial, split==F)
training_set_ca
##    Codigo.Pais           Pais       Ciudad Poblacion      DenPob  ArCiclista
## 1           ES          Spain    Barcelona   1620343  2.22857298  0.40404755
## 2           ES          Spain       Madrid   3223334 -0.03195466 -0.60455235
## 3           FR         France     Bordeaux    254436 -0.08020408 -0.13063191
## 4           FR         France        Lille    232787  0.25479080 -0.22784636
## 8           FR         France       Nantes    309346 -0.16485218  2.55491722
## 9           FR         France         Nice    340017 -0.18982338 -0.73822222
## 10          FR         France        Paris   2187526  3.23546219  0.84151257
## 14          GB United Kingdom     Bradford    537173 -0.85029021 -1.05416917
## 15          GB United Kingdom      Bristol    463405 -0.27933875 -0.11848011
## 16          GB United Kingdom    Edinburgh    518500 -0.75844702  0.02734156
## 17          GB United Kingdom      Glasgow    626410 -0.40842711 -0.60455235
## 18          GB United Kingdom        Leeds    789194 -0.85769692 -0.38581984
## 19          GB United Kingdom    Liverpool    494814 -0.37647245 -1.01771375
## 20          GB United Kingdom Inner London   3600203  1.16221848 -0.10632830
## 21          GB United Kingdom   Manchester    547627 -0.15829196 -0.42227526
## 24          NO         Norway         Oslo    681071 -0.86044799  0.56202103
##     ArBajaVel  PMPeatones PMCiclistas PMTPublico    PMVMotor PeatAuto CicAuto
## 1   1.8984595  0.66156039 -0.27804757  1.6644700 -1.74922903       22       5
## 2  -0.9238882  0.82660664 -0.80613791  0.2431521 -0.82481711      201      18
## 3  -0.2322143 -0.24619399  0.06486824 -1.2460786  1.11938825       13       5
## 4   1.3379651  0.66156039 -0.27804757 -1.1490604  0.33051850        5       1
## 8   0.2249265  0.24894476  0.75069985 -0.5669506  0.04365677       13       7
## 9  -0.7768081  1.32174540 -0.62096337 -1.0520421 -0.24320496       20       2
## 10  0.7695204  1.89940728  0.06486824  1.0823603 -2.46638335       58      14
## 14 -1.0868689 -1.56656400 -0.62096337  0.8883237  0.83252652       60      14
## 15  0.6462911 -0.24619399  3.83694212 -0.5669506 -0.17148953       25      28
## 16  1.7792053  0.82660664 -0.27804757 -0.2758958 -0.45835126       33      15
## 17 -0.8046341  0.24894476 -0.62096337  1.0823603 -0.88864385       61      23
## 18 -0.9517142 -1.64908713 -0.62096337  1.5674517  0.40223393       75      50
## 19 -0.2202889 -0.98890212 -0.27804757  0.4032323  0.68909566       71      27
## 20  2.2800727  0.08389851  0.06486824  1.3734151 -1.03207471      398     340
## 21  0.3799569 -1.15394837 -0.27804757 -0.4699323  1.40624998       40      34
## 24 -0.2401646  0.66156039  0.75069985  0.4032323 -1.03207471        9       6
##    V2RMSM V2RMAuto AutoSM AutoAuto Precipitacion       Temp         PBI
## 1      29       96      0        4   -1.15398383  2.2028201 -0.61130324
## 2      95      275     44       72   -2.03615396  1.0620475 -0.47866210
## 3       2       14      3        1    1.16683988  0.6342577 -0.47318053
## 4       2        7      2        6   -0.06734123 -0.4352167 -0.60434454
## 8       6       13      1        6    0.40404739  0.1708188 -0.45043750
## 9       6       50      5        6   -0.12549957  1.4185389 -0.44952287
## 10     48      132     12       24   -0.71075609  0.1351697  1.84223680
## 14      8       11     18       31    0.73034626 -0.9343047 -0.83772977
## 15      1       22      6       11    0.29752580 -0.5065149 -0.33657759
## 16      1       17      7       14   -0.30119896 -0.9699538  0.14738356
## 17      2       16     11       14    3.01076524 -0.8630064 -0.25264595
## 18     17       43     24       53    0.34527686 -0.9343047 -0.28572111
## 19      5       22      7       22    0.50873239 -0.6847607 -0.54684114
## 20     72      371     44      123   -0.92930899 -0.2569709  1.61735105
## 21      4       31      5       28    0.46098134 -0.7204098  0.03247566
## 24      2        2      2        2    0.30119896 -1.8611825  1.51526431
##    stPeatAuto stCicAuto  stV2RMSM stV2RMAuto  stAutoSM stAutoAuto
## 1    13.57737  3.085766 17.897445  59.246715  0.000000   2.468613
## 2    62.35779  5.584280 29.472590  85.315391 13.650463  22.337121
## 3    51.09340 19.651307  7.860523  55.023660 11.790784   3.930261
## 4    21.47886  4.295773  8.591545  30.070408  8.591545  25.774635
## 8    42.02414 22.628384 19.395758  42.024141  3.232626  19.395758
## 9    58.82059  5.882059 17.646177 147.051471 14.705147  17.646177
## 10   26.51397  6.399924 21.942596  60.342140  5.485649  10.971298
## 14  111.69586 26.062367 14.892781  20.477574 33.508758  57.709527
## 15   53.94849 60.422309  2.157940  47.474671 12.947638  23.737336
## 16   63.64513 28.929605  1.928640  32.786885 13.500482  27.000964
## 17   97.38031 36.717166  3.192797  25.542376 17.560384  22.349579
## 18   95.03367 63.355778 21.540965  54.485969 30.410774  67.157125
## 19  143.48826 54.565958 10.104807  44.461151 14.146730  44.461151
## 20  110.54932 94.439119 19.998872 103.049745 12.221533  34.164740
## 21   73.04242 62.086055  7.304242  56.607874  9.130302  51.129692
## 24   13.21448  8.809654  2.936551   2.936551  2.936551   2.936551

AutoSM

split <- sample.split(df_segvial$stAutoSM , SplitRatio = 2/3)
training_set_as <- subset(df_segvial, split==T)
test_set_as    <- subset(df_segvial, split==F)
training_set_as
##    Codigo.Pais           Pais       Ciudad Poblacion      DenPob  ArCiclista
## 1           ES          Spain    Barcelona   1620343  2.22857298  0.40404755
## 2           ES          Spain       Madrid   3223334 -0.03195466 -0.60455235
## 5           FR         France         Lyon    516092  1.11629688 -0.09417650
## 7           FR         France  Montpellier    285121 -0.10284745  0.61062825
## 8           FR         France       Nantes    309346 -0.16485218  2.55491722
## 9           FR         France         Nice    340017 -0.18982338 -0.73822222
## 10          FR         France        Paris   2187526  3.23546219  0.84151257
## 11          FR         France   Strasbourg    280966 -0.40038554  2.62782805
## 12          FR         France     Toulouse    479553 -0.30028915  1.14530772
## 13          GB United Kingdom   Birmingham   1141374 -0.25838834 -0.51948971
## 14          GB United Kingdom     Bradford    537173 -0.85029021 -1.05416917
## 15          GB United Kingdom      Bristol    463405 -0.27933875 -0.11848011
## 16          GB United Kingdom    Edinburgh    518500 -0.75844702  0.02734156
## 19          GB United Kingdom    Liverpool    494814 -0.37647245 -1.01771375
## 20          GB United Kingdom Inner London   3600203  1.16221848 -0.10632830
## 21          GB United Kingdom   Manchester    547627 -0.15829196 -0.42227526
##     ArBajaVel  PMPeatones PMCiclistas PMTPublico    PMVMotor PeatAuto CicAuto
## 1   1.8984595  0.66156039 -0.27804757  1.6644700 -1.74922903       22       5
## 2  -0.9238882  0.82660664 -0.80613791  0.2431521 -0.82481711      201      18
## 5  -0.0533331  0.82660664 -0.27804757 -0.2758958 -0.45835126       31      11
## 7  -0.6853800  0.16642164 -0.27804757 -1.3430969  0.90424195       21       4
## 8   0.2249265  0.24894476  0.75069985 -0.5669506  0.04365677       13       7
## 9  -0.7768081  1.32174540 -0.62096337 -1.0520421 -0.24320496       20       2
## 10  0.7695204  1.89940728  0.06486824  1.0823603 -2.46638335       58      14
## 11 -0.4389214  0.74408352  1.77944727 -0.9550238 -0.31492039        5       8
## 12  0.3481558 -0.24619399  0.06486824 -1.2460786  1.11938825       12       2
## 13 -0.4428966 -1.15394837 -0.27804757 -0.3729140  1.33453455      153      54
## 14 -1.0868689 -1.56656400 -0.62096337  0.8883237  0.83252652       60      14
## 15  0.6462911 -0.24619399  3.83694212 -0.5669506 -0.17148953       25      28
## 16  1.7792053  0.82660664 -0.27804757 -0.2758958 -0.45835126       33      15
## 19 -0.2202889 -0.98890212 -0.27804757  0.4032323  0.68909566       71      27
## 20  2.2800727  0.08389851  0.06486824  1.3734151 -1.03207471      398     340
## 21  0.3799569 -1.15394837 -0.27804757 -0.4699323  1.40624998       40      34
##    V2RMSM V2RMAuto AutoSM AutoAuto Precipitacion       Temp         PBI
## 1      29       96      0        4    -1.1539838  2.2028201 -0.61130324
## 2      95      275     44       72    -2.0361540  1.0620475 -0.47866210
## 5      12       39      8       21     0.4799593  0.1708188 -0.07473309
## 7       8       21      9       12    -0.7615681  1.0976966  3.19564902
## 8       6       13      1        6     0.4040474  0.1708188 -0.45043750
## 9       6       50      5        6    -0.1254996  1.4185389 -0.44952287
## 10     48      132     12       24    -0.7107561  0.1351697  1.84223680
## 11      4        8      4        3    -0.5417908 -0.3995675 -0.49330720
## 12      5       29      5       12    -0.7070829  0.8125034 -0.24376255
## 13      6       55     20      122     0.3146672 -0.8273573 -0.65236796
## 14      8       11     18       31     0.7303463 -0.9343047 -0.83772977
## 15      1       22      6       11     0.2975258 -0.5065149 -0.33657759
## 16      1       17      7       14    -0.3011990 -0.9699538  0.14738356
## 19      5       22      7       22     0.5087324 -0.6847607 -0.54684114
## 20     72      371     44      123    -0.9293090 -0.2569709  1.61735105
## 21      4       31      5       28     0.4609813 -0.7204098  0.03247566
##    stPeatAuto stCicAuto  stV2RMSM stV2RMAuto  stAutoSM stAutoAuto
## 1    13.57737  3.085766 17.897445   59.24672  0.000000   2.468613
## 2    62.35779  5.584280 29.472590   85.31539 13.650463  22.337121
## 5    60.06681 21.314029 23.251668   75.56792 15.501112  40.690420
## 7    73.65294 14.029131 28.058263   73.65294 31.565546  42.087394
## 8    42.02414 22.628384 19.395758   42.02414  3.232626  19.395758
## 9    58.82059  5.882059 17.646177  147.05147 14.705147  17.646177
## 10   26.51397  6.399924 21.942596   60.34214  5.485649  10.971298
## 11   17.79575 28.473196 14.236598   28.47320 14.236598  10.677449
## 12   25.02330  4.170550 10.426376   60.47298 10.426376  25.023303
## 13  134.04896 47.311398  5.256822   48.18754 17.522740 106.888715
## 14  111.69586 26.062367 14.892781   20.47757 33.508758  57.709527
## 15   53.94849 60.422309  2.157940   47.47467 12.947638  23.737336
## 16   63.64513 28.929605  1.928640   32.78689 13.500482  27.000964
## 19  143.48826 54.565958 10.104807   44.46115 14.146730  44.461151
## 20  110.54932 94.439119 19.998872  103.04974 12.221533  34.164740
## 21   73.04242 62.086055  7.304242   56.60787  9.130302  51.129692

AutoAuto

split <- sample.split(df_segvial$stAutoAuto, SplitRatio = 2/3)
training_set_aa <- subset(df_segvial, split==T)
test_set_aa    <- subset(df_segvial, split==F)
training_set_aa
##    Codigo.Pais           Pais      Ciudad Poblacion      DenPob  ArCiclista
## 1           ES          Spain   Barcelona   1620343  2.22857298  0.40404755
## 2           ES          Spain      Madrid   3223334 -0.03195466 -0.60455235
## 3           FR         France    Bordeaux    254436 -0.08020408 -0.13063191
## 4           FR         France       Lille    232787  0.25479080 -0.22784636
## 6           FR         France   Marseille    863310 -0.40609928 -0.88404389
## 7           FR         France Montpellier    285121 -0.10284745  0.61062825
## 8           FR         France      Nantes    309346 -0.16485218  2.55491722
## 9           FR         France        Nice    340017 -0.18982338 -0.73822222
## 11          FR         France  Strasbourg    280966 -0.40038554  2.62782805
## 13          GB United Kingdom  Birmingham   1141374 -0.25838834 -0.51948971
## 14          GB United Kingdom    Bradford    537173 -0.85029021 -1.05416917
## 16          GB United Kingdom   Edinburgh    518500 -0.75844702  0.02734156
## 18          GB United Kingdom       Leeds    789194 -0.85769692 -0.38581984
## 22          GB United Kingdom   Sheffield    582506 -0.82531902 -0.77467763
## 23          IT          Italy        Rome   2872800 -0.68776585 -1.09062459
## 24          NO         Norway        Oslo    681071 -0.86044799  0.56202103
##     ArBajaVel PMPeatones PMCiclistas PMTPublico    PMVMotor PeatAuto CicAuto
## 1   1.8984595  0.6615604 -0.27804757  1.6644700 -1.74922903       22       5
## 2  -0.9238882  0.8266066 -0.80613791  0.2431521 -0.82481711      201      18
## 3  -0.2322143 -0.2461940  0.06486824 -1.2460786  1.11938825       13       5
## 4   1.3379651  0.6615604 -0.27804757 -1.1490604  0.33051850        5       1
## 6  -0.7132059  0.8266066 -0.62096337 -1.0520421  0.18708763       56       9
## 7  -0.6853800  0.1664216 -0.27804757 -1.3430969  0.90424195       21       4
## 8   0.2249265  0.2489448  0.75069985 -0.5669506  0.04365677       13       7
## 9  -0.7768081  1.3217454 -0.62096337 -1.0520421 -0.24320496       20       2
## 11 -0.4389214  0.7440835  1.77944727 -0.9550238 -0.31492039        5       8
## 13 -0.4428966 -1.1539484 -0.27804757 -0.3729140  1.33453455      153      54
## 14 -1.0868689 -1.5665640 -0.62096337  0.8883237  0.83252652       60      14
## 16  1.7792053  0.8266066 -0.27804757 -0.2758958 -0.45835126       33      15
## 18 -0.9517142 -1.6490871 -0.62096337  1.5674517  0.40223393       75      50
## 22 -1.0272418 -1.1539484 -0.62096337  1.1793786  0.25880307       66      39
## 23 -1.0669932 -1.5995733 -0.62096337  0.6845853  1.01181510      249      15
## 24 -0.2401646  0.6615604  0.75069985  0.4032323 -1.03207471        9       6
##    V2RMSM V2RMAuto AutoSM AutoAuto Precipitacion       Temp        PBI
## 1      29       96      0        4   -1.15398383  2.2028201 -0.6113032
## 2      95      275     44       72   -2.03615396  1.0620475 -0.4786621
## 3       2       14      3        1    1.16683988  0.6342577 -0.4731805
## 4       2        7      2        6   -0.06734123 -0.4352167 -0.6043445
## 6      45      146     30       48   -1.45763156  1.2402932 -0.4490681
## 7       8       21      9       12   -0.76156811  1.0976966  3.1956490
## 8       6       13      1        6    0.40404739  0.1708188 -0.4504375
## 9       6       50      5        6   -0.12549957  1.4185389 -0.4495229
## 11      4        8      4        3   -0.54179082 -0.3995675 -0.4933072
## 13      6       55     20      122    0.31466721 -0.8273573 -0.6523680
## 14      8       11     18       31    0.73034626 -0.9343047 -0.8377298
## 16      1       17      7       14   -0.30119896 -0.9699538  0.1473836
## 18     17       43     24       53    0.34527686 -0.9343047 -0.2857211
## 22      4       30     28      112    0.49648853 -0.7204098 -0.7148510
## 23    143      346    138      217    0.27548686  1.1689949 -0.3953041
## 24      2        2      2        2    0.30119896 -1.8611825  1.5152643
##    stPeatAuto stCicAuto  stV2RMSM stV2RMAuto  stAutoSM stAutoAuto
## 1    13.57737  3.085766 17.897445  59.246715  0.000000   2.468613
## 2    62.35779  5.584280 29.472590  85.315391 13.650463  22.337121
## 3    51.09340 19.651307  7.860523  55.023660 11.790784   3.930261
## 4    21.47886  4.295773  8.591545  30.070408  8.591545  25.774635
## 6    64.86662 10.424992 52.124961 169.116540 34.749974  55.599958
## 7    73.65294 14.029131 28.058263  73.652940 31.565546  42.087394
## 8    42.02414 22.628384 19.395758  42.024141  3.232626  19.395758
## 9    58.82059  5.882059 17.646177 147.051471 14.705147  17.646177
## 11   17.79575 28.473196 14.236598  28.473196 14.236598  10.677449
## 13  134.04896 47.311398  5.256822  48.187535 17.522740 106.888715
## 14  111.69586 26.062367 14.892781  20.477574 33.508758  57.709527
## 16   63.64513 28.929605  1.928640  32.786885 13.500482  27.000964
## 18   95.03367 63.355778 21.540965  54.485969 30.410774  67.157125
## 22  113.30355 66.952100  6.866882  51.501615 48.068174 192.272698
## 23   86.67502  5.221387 49.777221 120.439989 48.036759  75.536062
## 24   13.21448  8.809654  2.936551   2.936551  2.936551   2.936551

V2RMSM

split <- sample.split(df_segvial$stV2RMSM , SplitRatio = 2/3)
training_set_v2rms <- subset(df_segvial, split==T)
test_set_v2rms   <- subset(df_segvial, split==F)
training_set_v2rms
##    Codigo.Pais           Pais       Ciudad Poblacion      DenPob  ArCiclista
## 1           ES          Spain    Barcelona   1620343  2.22857298  0.40404755
## 2           ES          Spain       Madrid   3223334 -0.03195466 -0.60455235
## 3           FR         France     Bordeaux    254436 -0.08020408 -0.13063191
## 5           FR         France         Lyon    516092  1.11629688 -0.09417650
## 6           FR         France    Marseille    863310 -0.40609928 -0.88404389
## 9           FR         France         Nice    340017 -0.18982338 -0.73822222
## 10          FR         France        Paris   2187526  3.23546219  0.84151257
## 11          FR         France   Strasbourg    280966 -0.40038554  2.62782805
## 13          GB United Kingdom   Birmingham   1141374 -0.25838834 -0.51948971
## 14          GB United Kingdom     Bradford    537173 -0.85029021 -1.05416917
## 16          GB United Kingdom    Edinburgh    518500 -0.75844702  0.02734156
## 17          GB United Kingdom      Glasgow    626410 -0.40842711 -0.60455235
## 19          GB United Kingdom    Liverpool    494814 -0.37647245 -1.01771375
## 20          GB United Kingdom Inner London   3600203  1.16221848 -0.10632830
## 23          IT          Italy         Rome   2872800 -0.68776585 -1.09062459
## 24          NO         Norway         Oslo    681071 -0.86044799  0.56202103
##     ArBajaVel  PMPeatones PMCiclistas PMTPublico   PMVMotor PeatAuto CicAuto
## 1   1.8984595  0.66156039 -0.27804757  1.6644700 -1.7492290       22       5
## 2  -0.9238882  0.82660664 -0.80613791  0.2431521 -0.8248171      201      18
## 3  -0.2322143 -0.24619399  0.06486824 -1.2460786  1.1193883       13       5
## 5  -0.0533331  0.82660664 -0.27804757 -0.2758958 -0.4583513       31      11
## 6  -0.7132059  0.82660664 -0.62096337 -1.0520421  0.1870876       56       9
## 9  -0.7768081  1.32174540 -0.62096337 -1.0520421 -0.2432050       20       2
## 10  0.7695204  1.89940728  0.06486824  1.0823603 -2.4663834       58      14
## 11 -0.4389214  0.74408352  1.77944727 -0.9550238 -0.3149204        5       8
## 13 -0.4428966 -1.15394837 -0.27804757 -0.3729140  1.3345345      153      54
## 14 -1.0868689 -1.56656400 -0.62096337  0.8883237  0.8325265       60      14
## 16  1.7792053  0.82660664 -0.27804757 -0.2758958 -0.4583513       33      15
## 17 -0.8046341  0.24894476 -0.62096337  1.0823603 -0.8886438       61      23
## 19 -0.2202889 -0.98890212 -0.27804757  0.4032323  0.6890957       71      27
## 20  2.2800727  0.08389851  0.06486824  1.3734151 -1.0320747      398     340
## 23 -1.0669932 -1.59957325 -0.62096337  0.6845853  1.0118151      249      15
## 24 -0.2401646  0.66156039  0.75069985  0.4032323 -1.0320747        9       6
##    V2RMSM V2RMAuto AutoSM AutoAuto Precipitacion       Temp         PBI
## 1      29       96      0        4    -1.1539838  2.2028201 -0.61130324
## 2      95      275     44       72    -2.0361540  1.0620475 -0.47866210
## 3       2       14      3        1     1.1668399  0.6342577 -0.47318053
## 5      12       39      8       21     0.4799593  0.1708188 -0.07473309
## 6      45      146     30       48    -1.4576316  1.2402932 -0.44906814
## 9       6       50      5        6    -0.1254996  1.4185389 -0.44952287
## 10     48      132     12       24    -0.7107561  0.1351697  1.84223680
## 11      4        8      4        3    -0.5417908 -0.3995675 -0.49330720
## 13      6       55     20      122     0.3146672 -0.8273573 -0.65236796
## 14      8       11     18       31     0.7303463 -0.9343047 -0.83772977
## 16      1       17      7       14    -0.3011990 -0.9699538  0.14738356
## 17      2       16     11       14     3.0107652 -0.8630064 -0.25264595
## 19      5       22      7       22     0.5087324 -0.6847607 -0.54684114
## 20     72      371     44      123    -0.9293090 -0.2569709  1.61735105
## 23    143      346    138      217     0.2754869  1.1689949 -0.39530410
## 24      2        2      2        2     0.3011990 -1.8611825  1.51526431
##    stPeatAuto stCicAuto  stV2RMSM stV2RMAuto  stAutoSM stAutoAuto
## 1    13.57737  3.085766 17.897445  59.246715  0.000000   2.468613
## 2    62.35779  5.584280 29.472590  85.315391 13.650463  22.337121
## 3    51.09340 19.651307  7.860523  55.023660 11.790784   3.930261
## 5    60.06681 21.314029 23.251668  75.567922 15.501112  40.690420
## 6    64.86662 10.424992 52.124961 169.116540 34.749974  55.599958
## 9    58.82059  5.882059 17.646177 147.051471 14.705147  17.646177
## 10   26.51397  6.399924 21.942596  60.342140  5.485649  10.971298
## 11   17.79575 28.473196 14.236598  28.473196 14.236598  10.677449
## 13  134.04896 47.311398  5.256822  48.187535 17.522740 106.888715
## 14  111.69586 26.062367 14.892781  20.477574 33.508758  57.709527
## 16   63.64513 28.929605  1.928640  32.786885 13.500482  27.000964
## 17   97.38031 36.717166  3.192797  25.542376 17.560384  22.349579
## 19  143.48826 54.565958 10.104807  44.461151 14.146730  44.461151
## 20  110.54932 94.439119 19.998872 103.049745 12.221533  34.164740
## 23   86.67502  5.221387 49.777221 120.439989 48.036759  75.536062
## 24   13.21448  8.809654  2.936551   2.936551  2.936551   2.936551

V2RMAuto

split <- sample.split(df_segvial$stV2RMAuto, SplitRatio = 2/3)
training_set_v2rma <- subset(df_segvial, split==T)
test_set_v2rma    <- subset(df_segvial, split==F)
training_set_v2rma
##    Codigo.Pais           Pais     Ciudad Poblacion      DenPob  ArCiclista
## 1           ES          Spain  Barcelona   1620343  2.22857298  0.40404755
## 2           ES          Spain     Madrid   3223334 -0.03195466 -0.60455235
## 5           FR         France       Lyon    516092  1.11629688 -0.09417650
## 6           FR         France  Marseille    863310 -0.40609928 -0.88404389
## 8           FR         France     Nantes    309346 -0.16485218  2.55491722
## 9           FR         France       Nice    340017 -0.18982338 -0.73822222
## 10          FR         France      Paris   2187526  3.23546219  0.84151257
## 11          FR         France Strasbourg    280966 -0.40038554  2.62782805
## 12          FR         France   Toulouse    479553 -0.30028915  1.14530772
## 14          GB United Kingdom   Bradford    537173 -0.85029021 -1.05416917
## 16          GB United Kingdom  Edinburgh    518500 -0.75844702  0.02734156
## 17          GB United Kingdom    Glasgow    626410 -0.40842711 -0.60455235
## 19          GB United Kingdom  Liverpool    494814 -0.37647245 -1.01771375
## 21          GB United Kingdom Manchester    547627 -0.15829196 -0.42227526
## 23          IT          Italy       Rome   2872800 -0.68776585 -1.09062459
## 24          NO         Norway       Oslo    681071 -0.86044799  0.56202103
##     ArBajaVel PMPeatones PMCiclistas PMTPublico    PMVMotor PeatAuto CicAuto
## 1   1.8984595  0.6615604 -0.27804757  1.6644700 -1.74922903       22       5
## 2  -0.9238882  0.8266066 -0.80613791  0.2431521 -0.82481711      201      18
## 5  -0.0533331  0.8266066 -0.27804757 -0.2758958 -0.45835126       31      11
## 6  -0.7132059  0.8266066 -0.62096337 -1.0520421  0.18708763       56       9
## 8   0.2249265  0.2489448  0.75069985 -0.5669506  0.04365677       13       7
## 9  -0.7768081  1.3217454 -0.62096337 -1.0520421 -0.24320496       20       2
## 10  0.7695204  1.8994073  0.06486824  1.0823603 -2.46638335       58      14
## 11 -0.4389214  0.7440835  1.77944727 -0.9550238 -0.31492039        5       8
## 12  0.3481558 -0.2461940  0.06486824 -1.2460786  1.11938825       12       2
## 14 -1.0868689 -1.5665640 -0.62096337  0.8883237  0.83252652       60      14
## 16  1.7792053  0.8266066 -0.27804757 -0.2758958 -0.45835126       33      15
## 17 -0.8046341  0.2489448 -0.62096337  1.0823603 -0.88864385       61      23
## 19 -0.2202889 -0.9889021 -0.27804757  0.4032323  0.68909566       71      27
## 21  0.3799569 -1.1539484 -0.27804757 -0.4699323  1.40624998       40      34
## 23 -1.0669932 -1.5995733 -0.62096337  0.6845853  1.01181510      249      15
## 24 -0.2401646  0.6615604  0.75069985  0.4032323 -1.03207471        9       6
##    V2RMSM V2RMAuto AutoSM AutoAuto Precipitacion       Temp         PBI
## 1      29       96      0        4    -1.1539838  2.2028201 -0.61130324
## 2      95      275     44       72    -2.0361540  1.0620475 -0.47866210
## 5      12       39      8       21     0.4799593  0.1708188 -0.07473309
## 6      45      146     30       48    -1.4576316  1.2402932 -0.44906814
## 8       6       13      1        6     0.4040474  0.1708188 -0.45043750
## 9       6       50      5        6    -0.1254996  1.4185389 -0.44952287
## 10     48      132     12       24    -0.7107561  0.1351697  1.84223680
## 11      4        8      4        3    -0.5417908 -0.3995675 -0.49330720
## 12      5       29      5       12    -0.7070829  0.8125034 -0.24376255
## 14      8       11     18       31     0.7303463 -0.9343047 -0.83772977
## 16      1       17      7       14    -0.3011990 -0.9699538  0.14738356
## 17      2       16     11       14     3.0107652 -0.8630064 -0.25264595
## 19      5       22      7       22     0.5087324 -0.6847607 -0.54684114
## 21      4       31      5       28     0.4609813 -0.7204098  0.03247566
## 23    143      346    138      217     0.2754869  1.1689949 -0.39530410
## 24      2        2      2        2     0.3011990 -1.8611825  1.51526431
##    stPeatAuto stCicAuto  stV2RMSM stV2RMAuto  stAutoSM stAutoAuto
## 1    13.57737  3.085766 17.897445  59.246715  0.000000   2.468613
## 2    62.35779  5.584280 29.472590  85.315391 13.650463  22.337121
## 5    60.06681 21.314029 23.251668  75.567922 15.501112  40.690420
## 6    64.86662 10.424992 52.124961 169.116540 34.749974  55.599958
## 8    42.02414 22.628384 19.395758  42.024141  3.232626  19.395758
## 9    58.82059  5.882059 17.646177 147.051471 14.705147  17.646177
## 10   26.51397  6.399924 21.942596  60.342140  5.485649  10.971298
## 11   17.79575 28.473196 14.236598  28.473196 14.236598  10.677449
## 12   25.02330  4.170550 10.426376  60.472982 10.426376  25.023303
## 14  111.69586 26.062367 14.892781  20.477574 33.508758  57.709527
## 16   63.64513 28.929605  1.928640  32.786885 13.500482  27.000964
## 17   97.38031 36.717166  3.192797  25.542376 17.560384  22.349579
## 19  143.48826 54.565958 10.104807  44.461151 14.146730  44.461151
## 21   73.04242 62.086055  7.304242  56.607874  9.130302  51.129692
## 23   86.67502  5.221387 49.777221 120.439989 48.036759  75.536062
## 24   13.21448  8.809654  2.936551   2.936551  2.936551   2.936551

Dado que tenemos que realizar el mismo procedimiento para cada una de las variables de respuesta, se trabajara todo de a una pero siguiendo los mismos pasos que son:

  • Seleccionar el mejor modelo usando lasso

  • Obtener los coeficientes

  • Calcular el MSE

PeatAuto

# Matrices de entrenamiento y test

x_train_pa <- model.matrix(stPeatAuto~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = training_set_pa)[, -1]
y_train_pa <- training_set_pa$stPeatAuto

x_test_pa <- model.matrix(stPeatAuto~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = test_set_pa)[, -1]
y_test_pa <- test_set_pa$stPeatAuto
# Creación y entrenamiento del modelo

# Para obtener un ajuste con regularización Lasso se indica argumento alpha=1.
# Si no se especifica valor de lambda, se selecciona un rango automático.

modelo_pa <- glmnet(
            x           = x_train_pa,
            y           = y_train_pa,
            alpha       = 1,
            nlambda     = 100,
            standardize = TRUE
          )
# Evolución de los coeficientes en función de lambda

regularizacion_pa <- modelo_pa$beta %>% 
                  as.matrix() %>%
                  t() %>% 
                  as_tibble() %>%
                  mutate(lambda = modelo_pa$lambda)

regularizacion_pa <- regularizacion_pa %>%
                   pivot_longer(
                     cols = !lambda, 
                     names_to = "predictor",
                     values_to = "coeficientes"
                   )

regularizacion_pa %>%
  ggplot(aes(x = lambda, y = coeficientes, color = predictor)) +
  geom_line() +
  scale_x_log10(
    breaks = trans_breaks("log10", function(x) 10^x),
    labels = trans_format("log10", math_format(10^.x))
  ) +
  labs(title = "Coeficientes del modelo en función de la regularización") +
  theme_bw() +
  theme(legend.position = "none")

Puede verse como, a medida que aumenta el valor de lambda, la regularización es mayor y más predictores quedan excluidos (su coeficiente es 0).

Para identificar el valor de lambda que da lugar al mejor modelo, se puede recurrir a validación cruzada con la función cv.glmnet().

# Evolución del error en función de lambda

set.seed(123)
cv_error_pa <- cv.glmnet(
              x      = x_train_pa,
              y      = y_train_pa,
              alpha  = 1,
              nfolds = 10,
              type.measure = "mse",
              standardize  = TRUE
           )
## Warning: Option grouped=FALSE enforced in cv.glmnet, since < 3 observations per
## fold
plot(cv_error_pa)

# Mejor valor lambda encontrado
paste("Mejor valor de lambda encontrado:", cv_error_pa$lambda.min)
## [1] "Mejor valor de lambda encontrado: 11.353822308282"
# Mejor valor lambda encontrado + 1sd

# Mayor valor de lambda con el que el test-error no se aleja más de 1sd del mínimo.
paste("Mejor valor de lambda encontrado + 1 desviación estándar:", cv_error_pa$lambda.1se)
## [1] "Mejor valor de lambda encontrado + 1 desviación estándar: 18.0784959001297"

Se entrena de nuevo el modelo, esta vez empleando el mayor valor de lambda cuyo error está a menos de una desviación típica del mínimo encontrado en la validación cruzada.

# Mejor modelo lambda óptimo + 1sd

modelo_pa <- glmnet(
            x           = x_train_pa,
            y           = y_train_pa,
            alpha       = 1,
            lambda      = cv_error_pa$lambda.min,
            standardize = TRUE
          )
# Coeficientes del modelo

df_coeficientes_pa <- coef(modelo_pa) %>%
                   as.matrix() %>%
                   as_tibble(rownames = "predictor") %>%
                   rename(coeficiente = s0)

df_coeficientes_pa %>%
  filter(predictor != "(Intercept)") %>%
  ggplot(aes(x = predictor, y = coeficiente)) +
  geom_col() +
  labs(title = "Coeficientes del modelo Lasso") +
  theme_bw() +
  theme(axis.text.x = element_text(size = 6, angle = 45))

df_coeficientes_pa %>%
  filter(
    predictor != "(Intercept)",
    coeficiente != 0
) 
## # A tibble: 2 × 2
##   predictor  coeficiente
##   <chr>            <dbl>
## 1 PMPeatones       -8.51
## 2 ArCiclista      -14.6

De los 10 predictores disponibles, el modelo final solo incluye 2

# Predicciones de entrenamiento

predicciones_train_pa <- predict(modelo_pa, newx = x_train_pa)

# MSE de entrenamiento

training_mse_pa <- mean((predicciones_train_pa - y_train_pa)^2)
paste("Error (mse) de entrenamiento:", training_mse_pa)
## [1] "Error (mse) de entrenamiento: 528.745189314674"
# Predicciones de test

predicciones_test_pa <- predict(modelo_pa, newx = x_test_pa)

# MSE de test

test_mse_lasso_pa <- mean((predicciones_test_pa - y_test_pa)^2)
paste("Error (mse) de test:", test_mse_lasso_pa)
## [1] "Error (mse) de test: 1003.63880115104"

CicAuto

# Matrices de entrenamiento y test

x_train_ca <- model.matrix(stCicAuto~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = training_set_ca)[, -1]
y_train_ca <- training_set_ca$stCicAuto

x_test_ca <- model.matrix(stCicAuto~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = test_set_ca)[, -1]
y_test_ca <- test_set_ca$stCicAuto
# Creación y entrenamiento del modelo

modelo_ca <- glmnet(
            x           = x_train_ca,
            y           = y_train_ca,
            alpha       = 1,
            nlambda     = 100,
            standardize = TRUE
          )
# Evolución de los coeficientes en función de lambda

regularizacion_ca <- modelo_ca$beta %>% 
                  as.matrix() %>%
                  t() %>% 
                  as_tibble() %>%
                  mutate(lambda = modelo_ca$lambda)

regularizacion_ca <- regularizacion_ca %>%
                   pivot_longer(
                     cols = !lambda, 
                     names_to = "predictor",
                     values_to = "coeficientes"
                   )

regularizacion_ca %>%
  ggplot(aes(x = lambda, y = coeficientes, color = predictor)) +
  geom_line() +
  scale_x_log10(
    breaks = trans_breaks("log10", function(x) 10^x),
    labels = trans_format("log10", math_format(10^.x))
  ) +
  labs(title = "Coeficientes del modelo en función de la regularización") +
  theme_bw() +
  theme(legend.position = "none")


# Evolución del error en función de lambda


cv_error_ca <- cv.glmnet(
              x      = x_train_ca,
              y      = y_train_ca,
              alpha  = 1,
              nfolds = 10,
              type.measure = "mse",
              standardize  = TRUE
           )
## Warning: Option grouped=FALSE enforced in cv.glmnet, since < 3 observations per
## fold
plot(cv_error_ca)

# Mejor valor lambda encontrado
paste("Mejor valor de lambda encontrado:", cv_error_ca$lambda.min)
## [1] "Mejor valor de lambda encontrado: 8.53568813301055"
# Mejor valor lambda encontrado + 1sd

paste("Mejor valor de lambda encontrado + 1 desviación estándar:", cv_error_ca$lambda.1se)
## [1] "Mejor valor de lambda encontrado + 1 desviación estándar: 16.370683860737"

No utilizamos el mejor modelo lambda óptimo + 1sd , dado que si hacemos eso no tenemos ningun coeficiente y nos queda unicamente el intercept

# Mejor modelo lambda óptimo min 

modelo_ca <- glmnet(
            x           = x_train_ca,
            y           = y_train_ca,
            alpha       = 1,
            lambda      = cv_error_ca$lambda.min,
            standardize = TRUE
          )
# Coeficientes del modelo

df_coeficientes_ca <- coef(modelo_ca) %>%
                   as.matrix() %>%
                   as_tibble(rownames = "predictor") %>%
                   rename(coeficiente = s0)

df_coeficientes_ca %>%
  filter(predictor != "(Intercept)") %>%
  ggplot(aes(x = predictor, y = coeficiente)) +
  geom_col() +
  labs(title = "Coeficientes del modelo Lasso") +
  theme_bw() +
  theme(axis.text.x = element_text(size = 6, angle = 45))

df_coeficientes_ca %>%
  filter(
    predictor != "(Intercept)",
    coeficiente != 0
) 
## # A tibble: 1 × 2
##   predictor  coeficiente
##   <chr>            <dbl>
## 1 PMPeatones       -7.93
# Predicciones de entrenamiento

predicciones_train_ca <- predict(modelo_ca, newx = x_train_ca)

# MSE de entrenamiento

training_mse_ca <- mean((predicciones_train_ca - y_train_ca)^2)
paste("Error (mse) de entrenamiento:", training_mse_ca)
## [1] "Error (mse) de entrenamiento: 528.783266570171"
# Predicciones de test

predicciones_test_ca <- predict(modelo_ca, newx = x_test_ca)

# MSE de test

test_mse_lasso_ca <- mean((predicciones_test_ca - y_test_ca)^2)
paste("Error (mse) de test:", test_mse_lasso_ca)
## [1] "Error (mse) de test: 463.143134611149"

AutoSM

# Matrices de entrenamiento y test

x_train_as <- model.matrix(stAutoSM ~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = training_set_as)[, -1]
y_train_as <- training_set_as$stAutoSM

x_test_as <- model.matrix(stAutoSM~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = test_set_as)[, -1]
y_test_as <- test_set_as$stAutoSM
# Creación y entrenamiento del modelo

modelo_as <- glmnet(
            x           = x_train_as,
            y           = y_train_as,
            alpha       = 1,
            nlambda     = 100,
            standardize = TRUE
          )
# Evolución de los coeficientes en función de lambda

regularizacion_as <- modelo_as$beta %>% 
                  as.matrix() %>%
                  t() %>% 
                  as_tibble() %>%
                  mutate(lambda = modelo_as$lambda)

regularizacion_as <- regularizacion_as %>%
                   pivot_longer(
                     cols = !lambda, 
                     names_to = "predictor",
                     values_to = "coeficientes"
                   )

regularizacion_as %>%
  ggplot(aes(x = lambda, y = coeficientes, color = predictor)) +
  geom_line() +
  scale_x_log10(
    breaks = trans_breaks("log10", function(x) 10^x),
    labels = trans_format("log10", math_format(10^.x))
  ) +
  labs(title = "Coeficientes del modelo en función de la regularización") +
  theme_bw() +
  theme(legend.position = "none")


# Evolución del error en función de lambda

cv_error_as <- cv.glmnet(
              x      = x_train_as,
              y      = y_train_as,
              alpha  = 1,
              nfolds = 10,
              type.measure = "mse",
              standardize  = TRUE
           )
## Warning: Option grouped=FALSE enforced in cv.glmnet, since < 3 observations per
## fold
plot(cv_error_as)

# Mejor valor lambda encontrado
paste("Mejor valor de lambda encontrado:", cv_error_as$lambda.min)
## [1] "Mejor valor de lambda encontrado: 0.925567531378252"
# Mejor valor lambda encontrado + 1sd

paste("Mejor valor de lambda encontrado + 1 desviación estándar:", cv_error_as$lambda.1se)
## [1] "Mejor valor de lambda encontrado + 1 desviación estándar: 4.93947553313584"
# Mejor modelo lambda minimo

modelo_as <- glmnet(
            x           = x_train_as,
            y           = y_train_as,
            alpha       = 1,
            lambda      = cv_error_as$lambda.min,
            standardize = TRUE
          )
# Coeficientes del modelo

df_coeficientes_as <- coef(modelo_as) %>%
                   as.matrix() %>%
                   as_tibble(rownames = "predictor") %>%
                   rename(coeficiente = s0)

df_coeficientes_as %>%
  filter(predictor != "(Intercept)") %>%
  ggplot(aes(x = predictor, y = coeficiente)) +
  geom_col() +
  labs(title = "Coeficientes del modelo Lasso") +
  theme_bw() +
  theme(axis.text.x = element_text(size = 6, angle = 45))

df_coeficientes_as %>%
  filter(
    predictor != "(Intercept)",
    coeficiente != 0
) 
## # A tibble: 6 × 2
##   predictor  coeficiente
##   <chr>            <dbl>
## 1 PBI              2.68 
## 2 Temp            -0.155
## 3 PMPeatones      -0.745
## 4 ArBajaVel       -3.18 
## 5 ArCiclista      -1.42 
## 6 DenPob          -2.09
# Predicciones de entrenamiento

predicciones_train_as <- predict(modelo_as, newx = x_train_pa)

# MSE de entrenamiento

training_mse_as <- mean((predicciones_train_as - y_train_as)^2)
paste("Error (mse) de entrenamiento:", training_mse_as)
## [1] "Error (mse) de entrenamiento: 116.908401559133"
# Predicciones de test

predicciones_test_as <- predict(modelo_as, newx = x_test_as)

# MSE de test

test_mse_lasso_as <- mean((predicciones_test_as - y_test_as)^2)
paste("Error (mse) de test:", test_mse_lasso_as)
## [1] "Error (mse) de test: 272.172650464871"

AutoAuto

# Matrices de entrenamiento y test

x_train_aa <- model.matrix(stAutoAuto~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = training_set_aa)[, -1]
y_train_aa <- training_set_aa$stAutoAuto

x_test_aa <- model.matrix(stAutoAuto~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = test_set_aa)[, -1]
y_test_aa <- test_set_pa$stAutoAuto
# Creación y entrenamiento del modelo

modelo_aa <- glmnet(
            x           = x_train_aa,
            y           = y_train_aa,
            alpha       = 1,
            nlambda     = 100,
            standardize = TRUE
          )
# Evolución de los coeficientes en función de lambda

regularizacion_aa <- modelo_aa$beta %>% 
                  as.matrix() %>%
                  t() %>% 
                  as_tibble() %>%
                  mutate(lambda = modelo_aa$lambda)

regularizacion_aa <- regularizacion_aa %>%
                   pivot_longer(
                     cols = !lambda, 
                     names_to = "predictor",
                     values_to = "coeficientes"
                   )

regularizacion_pa %>%
  ggplot(aes(x = lambda, y = coeficientes, color = predictor)) +
  geom_line() +
  scale_x_log10(
    breaks = trans_breaks("log10", function(x) 10^x),
    labels = trans_format("log10", math_format(10^.x))
  ) +
  labs(title = "Coeficientes del modelo en función de la regularización") +
  theme_bw() +
  theme(legend.position = "none")

# Evolución del error en función de lambda

cv_error_aa <- cv.glmnet(
              x      = x_train_aa,
              y      = y_train_aa,
              alpha  = 1,
              nfolds = 10,
              type.measure = "mse",
              standardize  = TRUE
           )
## Warning: Option grouped=FALSE enforced in cv.glmnet, since < 3 observations per
## fold
plot(cv_error_pa)

# Mejor valor lambda encontrado
paste("Mejor valor de lambda encontrado:", cv_error_aa$lambda.min)
## [1] "Mejor valor de lambda encontrado: 12.2103925497714"
# Mejor valor lambda encontrado + 1sd
paste("Mejor valor de lambda encontrado + 1 desviación estándar:", cv_error_aa$lambda.1se)
## [1] "Mejor valor de lambda encontrado + 1 desviación estándar: 30.9577957281277"
# Mejor modelo lambda óptimo + 1sd

modelo_aa <- glmnet(
            x           = x_train_aa,
            y           = y_train_aa,
            alpha       = 1,
            lambda      = cv_error_aa$lambda.min,
            standardize = TRUE
          )
# Coeficientes del modelo

df_coeficientes_aa <- coef(modelo_aa) %>%
                   as.matrix() %>%
                   as_tibble(rownames = "predictor") %>%
                   rename(coeficiente = s0)

df_coeficientes_aa %>%
  filter(predictor != "(Intercept)") %>%
  ggplot(aes(x = predictor, y = coeficiente)) +
  geom_col() +
  labs(title = "Coeficientes del modelo Lasso") +
  theme_bw() +
  theme(axis.text.x = element_text(size = 6, angle = 45))

df_coeficientes_aa %>%
  filter(
    predictor != "(Intercept)",
    coeficiente != 0
) 
## # A tibble: 3 × 2
##   predictor   coeficiente
##   <chr>             <dbl>
## 1 PMCiclistas       -1.23
## 2 PMPeatones       -18.0 
## 3 ArCiclista        -1.06
# Predicciones de entrenamiento

predicciones_train_aa <- predict(modelo_aa, newx = x_train_aa)

# MSE de entrenamiento

training_mse_aa <- mean((predicciones_train_aa - y_train_aa)^2)
paste("Error (mse) de entrenamiento:", training_mse_aa)
## [1] "Error (mse) de entrenamiento: 1422.62892415536"
# Predicciones de test

predicciones_test_aa <- predict(modelo_aa, newx = x_test_aa)

# MSE de test

test_mse_lasso_aa <- mean((predicciones_test_aa - y_test_aa)^2)
paste("Error (mse) de test:", test_mse_lasso_aa)
## [1] "Error (mse) de test: 581.313261960649"

V2RMSM

# Matrices de entrenamiento y test

x_train_v2rms <- model.matrix(stV2RMSM~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = training_set_v2rms)[, -1]
y_train_v2rms <- training_set_v2rms$stV2RMSM

x_test_v2rms <- model.matrix(stV2RMSM~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = test_set_v2rms)[, -1]
y_test_v2rms <- test_set_v2rms$stV2RMSM
# Creación y entrenamiento del modelo

modelo_v2rms <- glmnet(
            x           = x_train_v2rms,
            y           = y_train_v2rms,
            alpha       = 1,
            nlambda     = 100,
            standardize = TRUE
          )
# Evolución de los coeficientes en función de lambda

regularizacion_v2rms <- modelo_v2rms$beta %>% 
                  as.matrix() %>%
                  t() %>% 
                  as_tibble() %>%
                  mutate(lambda = modelo_v2rms$lambda)

regularizacion_v2rms <- regularizacion_v2rms %>%
                   pivot_longer(
                     cols = !lambda, 
                     names_to = "predictor",
                     values_to = "coeficientes"
                   )

regularizacion_v2rms %>%
  ggplot(aes(x = lambda, y = coeficientes, color = predictor)) +
  geom_line() +
  scale_x_log10(
    breaks = trans_breaks("log10", function(x) 10^x),
    labels = trans_format("log10", math_format(10^.x))
  ) +
  labs(title = "Coeficientes del modelo en función de la regularización") +
  theme_bw() +
  theme(legend.position = "none")


# Evolución del error en función de lambda

cv_error_v2rms <- cv.glmnet(
              x      = x_train_v2rms,
              y      = y_train_v2rms,
              alpha  = 1,
              nfolds = 10,
              type.measure = "mse",
              standardize  = TRUE
           )
## Warning: Option grouped=FALSE enforced in cv.glmnet, since < 3 observations per
## fold
plot(cv_error_v2rms)

# Mejor valor lambda encontrado
paste("Mejor valor de lambda encontrado:", cv_error_v2rms$lambda.min)
## [1] "Mejor valor de lambda encontrado: 3.69457710861108"
# Mejor valor lambda encontrado + 1sd

paste("Mejor valor de lambda encontrado + 1 desviación estándar:", cv_error_v2rms$lambda.1se)
## [1] "Mejor valor de lambda encontrado + 1 desviación estándar: 9.3670996214073"
# Mejor modelo lambda minimo

modelo_v2rms <- glmnet(
            x           = x_train_v2rms,
            y           = y_train_v2rms,
            alpha       = 1,
            lambda      = cv_error_v2rms$lambda.min,
            standardize = TRUE
          )
# Coeficientes del modelo

df_coeficientes_v2rms <- coef(modelo_v2rms) %>%
                   as.matrix() %>%
                   as_tibble(rownames = "predictor") %>%
                   rename(coeficiente = s0)

df_coeficientes_v2rms %>%
  filter(predictor != "(Intercept)") %>%
  ggplot(aes(x = predictor, y = coeficiente)) +
  geom_col() +
  labs(title = "Coeficientes del modelo Lasso") +
  theme_bw() +
  theme(axis.text.x = element_text(size = 6, angle = 45))

df_coeficientes_v2rms %>%
  filter(
    predictor != "(Intercept)",
    coeficiente != 0
) 
## # A tibble: 3 × 2
##   predictor     coeficiente
##   <chr>               <dbl>
## 1 Precipitacion      -0.945
## 2 Temp                4.79 
## 3 ArBajaVel          -0.283
# Predicciones de entrenamiento

predicciones_train_v2rms <- predict(modelo_v2rms, newx = x_train_v2rms)

# MSE de entrenamiento

training_mse_v2rms <- mean((predicciones_train_v2rms - y_train_v2rms)^2)
paste("Error (mse) de entrenamiento:", training_mse_v2rms)
## [1] "Error (mse) de entrenamiento: 131.558222446041"
# Predicciones de test

predicciones_test_v2rms <- predict(modelo_v2rms, newx = x_test_v2rms)

# MSE de test

test_mse_lasso_v2rms <- mean((predicciones_test_v2rms - y_test_v2rms)^2)
paste("Error (mse) de test:", test_mse_lasso_v2rms)
## [1] "Error (mse) de test: 67.0569785191742"

V2RMAuto

# Matrices de entrenamiento y test

x_train_v2rma <- model.matrix(stV2RMAuto~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = training_set_v2rma)[, -1]
y_train_v2rma <- training_set_pa$stV2RMAuto

x_test_v2rma <- model.matrix(stPeatAuto~ Precipitacion+PBI+Temp+PMVMotor+PMTPublico+PMCiclistas+PMPeatones+ArBajaVel+ArCiclista+DenPob, data = test_set_v2rma)[, -1]
y_test_v2rma <- test_set_v2rma$stV2RMAuto
# Creación y entrenamiento del modelo

modelo_v2rma <- glmnet(
            x           = x_train_v2rma,
            y           = y_train_v2rma,
            alpha       = 1,
            nlambda     = 100,
            standardize = TRUE
          )
# Evolución de los coeficientes en función de lambda

regularizacion_v2rma <- modelo_v2rma$beta %>% 
                  as.matrix() %>%
                  t() %>% 
                  as_tibble() %>%
                  mutate(lambda = modelo_v2rma$lambda)

regularizacion_v2rma <- regularizacion_v2rma %>%
                   pivot_longer(
                     cols = !lambda, 
                     names_to = "predictor",
                     values_to = "coeficientes"
                   )

regularizacion_v2rma %>%
  ggplot(aes(x = lambda, y = coeficientes, color = predictor)) +
  geom_line() +
  scale_x_log10(
    breaks = trans_breaks("log10", function(x) 10^x),
    labels = trans_format("log10", math_format(10^.x))
  ) +
  labs(title = "Coeficientes del modelo en función de la regularización") +
  theme_bw() +
  theme(legend.position = "none")


# Evolución del error en función de lambda

cv_error_v2rma <- cv.glmnet(
              x      = x_train_v2rma,
              y      = y_train_v2rma,
              alpha  = 1,
              nfolds = 10,
              type.measure = "mse",
              standardize  = TRUE
           )
## Warning: from glmnet C++ code (error code -95); Convergence for 95th lambda
## value not reached after maxit=100000 iterations; solutions for larger lambdas
## returned
## Warning: Option grouped=FALSE enforced in cv.glmnet, since < 3 observations per
## fold
plot(cv_error_v2rma)

# Mejor valor lambda encontrado
paste("Mejor valor de lambda encontrado:", cv_error_v2rma$lambda.min)
## [1] "Mejor valor de lambda encontrado: 10.5720911630665"
# Mejor valor lambda encontrado + 1sd

paste("Mejor valor de lambda encontrado + 1 desviación estándar:", cv_error_v2rma$lambda.1se)
## [1] "Mejor valor de lambda encontrado + 1 desviación estándar: 20.2763221289822"
# Mejor modelo lambda minimo

modelo_v2rma <- glmnet(
            x           = x_train_v2rma,
            y           = y_train_v2rma,
            alpha       = 1,
            lambda      = cv_error_v2rma$lambda.min,
            standardize = TRUE
          )
# Coeficientes del modelo

df_coeficientes_v2rma <- coef(modelo_v2rma) %>%
                   as.matrix() %>%
                   as_tibble(rownames = "predictor") %>%
                   rename(coeficiente = s0)

df_coeficientes_v2rma %>%
  filter(predictor != "(Intercept)") %>%
  ggplot(aes(x = predictor, y = coeficiente)) +
  geom_col() +
  labs(title = "Coeficientes del modelo Lasso") +
  theme_bw() +
  theme(axis.text.x = element_text(size = 6, angle = 45))

df_coeficientes_v2rma %>%
  filter(
    predictor != "(Intercept)",
    coeficiente != 0
) 
## # A tibble: 1 × 2
##   predictor coeficiente
##   <chr>           <dbl>
## 1 Temp             9.01
# Predicciones de entrenamiento

predicciones_train_v2rma <- predict(modelo_v2rma, newx = x_train_v2rma)

# MSE de entrenamiento

training_mse_v2rma <- mean((predicciones_train_v2rma - y_train_v2rma)^2)
paste("Error (mse) de entrenamiento:", training_mse_v2rma)
## [1] "Error (mse) de entrenamiento: 1478.42574970098"
# Predicciones de test

predicciones_test_v2rma <- predict(modelo_v2rma, newx = x_test_v2rma)

# MSE de test

test_mse_lasso_v2rma <- mean((predicciones_test_v2rma - y_test_v2rma)^2)
paste("Error (mse) de test:", test_mse_lasso_v2rma)
## [1] "Error (mse) de test: 370.838806523125"

Comparacion

df_comparacion <- data.frame(
                    modelo = c("PeatAuto", "Cicauto", "AutoSM", "AutoAuto", "V2RMSM","V2RMAuto"),
                    mse    = c(test_mse_lasso_pa, test_mse_lasso_ca, test_mse_lasso_as,test_mse_lasso_aa, test_mse_lasso_v2rms, test_mse_lasso_v2rma)
)

ggplot(data = df_comparacion, aes(x = modelo, y = mse)) +
  geom_col(width = 0.5) +
  geom_text(aes(label = round(mse, 2)), vjust = -0.1) +
  theme_bw() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1))

Modelos

\[ \hat{PeatAuto} = 73.81-8.8\hat{PMPeatones}-10.8\hat{ArCiclista}\] \[\hat{CicAuto} = 22.6\] \[ \begin{multline} \hat{AutoSM} = 17.85-1.55\hat{PMPeatones}-4.86\hat{ArBajaVel}\\ -0.53\hat{ArCiclista}-0.44\hat{DenPob} \end{multline} \] \[ \begin{multline} \hat{AutoAuto} = 30.09-16.5\hat{Precipitacion}-0.27\hat{PBI}\\ -5.8\hat{Temp}-0.48\hat{PMCiclista}+0.15\hat{PMPeatones}\\ -5.15\hat{ArCiclista} \end{multline}\] \[ \begin{multline} \hat{V2RMSM} = 15.06-4.04\hat{Precipitacion}+3.5\hat{Temp}\\ +14.27\hat{PMVMotor}-1.22\hat{PMCiclista}\\ -12.86\hat{PMPeatones}-4.06\hat{ArBajaVel} \end{multline} \] \[ \hat{V2RMAuto} = 67.95+22.47\hat{Temp}-7.4\hat{ArCiclista} \]

Para los peatones y ciclistas se puede ver que un incremento en el área de ambos contribuirá a tener un menor índice. Para los casos donde están involucrados vehículos, se observa que la temperatura y las precipitaciones van a tener influencia en algunos casos de incremento, como para los indicadores de V2R y la temperatura y en otros de reducción, tal y como sucede con el MDS de AutoAuto para las precipitaciones, cabe destacar que uno no puede controlar el aumento o disminución de estos factores, pero sí se puede incentivar ciertas medidas considerándolos. Continuando en la línea de vehículos, los incrementos vinculados a zonas o áreas en la gran mayoría de estas métricas colaboran en forma de disminución, por lo que sería recomendable considerar acciones vinculadas a esto.

Recomendaciones

El objetivo principal de este trabajo es poder identificar políticas o accionables que permitan alcanzar nivel de fatalidad y accidentes de tránsito violentos cero, en diversas urbes. Algunas medidas que se podrían evaluar son:

  • Incrementar las zonas de bicisenda y baja velocidad, considerando el flujo tanto vehicular como peatonal y de ciclistas. La sugerencia es hacer un análisis y un plan en pasos para aumentar el área de estas zonas, principalmente donde hay mayor uso por parte de ciclistas y peatones que están más vulnerables a la hora de un accidente vial.

  • Cuando llueve se podría declarar algunas zonas peligrosas como de baja velocidad, considerando que es más complejo frenar en superficies resbaladizas.

  • Incorporar alguna normativa vial para ceder el paso a peatones y bicicletas, que en caso de no cumplirse sea multado. Dado que es probable que se respete con mayor rigurosidad en aquellos países que no es una costumbre esto.

  • Incentivar el uso de bicicletas o caminar, cuando la temperatura es adecuada con alguna política de prioridad de paso y/o en caso de contar con bicicletas públicas o de alquiler que estas tengan alguna tarifa diferencial o gratis

  • Política de pull para los casos que esté lloviendo o haga mucho calor cosa de reducir el volumen de autos

  • Transformar ciertas zonas sólo de uso peatonal o bicicleta en cierto momento del día o un día específico a la semana, esto es una forma más económica de aumentar el área de estos agentes sin necesidad de una inversión tan grande como podrían serlo áreas específicas para estos.